Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-check checkbox 1

Status
Not open for further replies.

Slippenos

MIS
Apr 22, 2005
333
US
Salesmen fill out this form I created and frequently enter a date for a particular option without checking the option. If they fill in the date without checking the checkbox, is there a way to auto-check it?

I assumed the onKeyPress event was the best choice. For the date- I am using Popup Calendar.

...

Code:
<html><body>

<script language = "javascript">
<!--



function autoCheck( ) 

	{

if ( drf.Sample.checked == false 
	&& drf.SampleDate.value.length !=0 )
		
		{

		//drf.Sample.checked == true;

		}

	}

//-->
</script>



<form name = "drf">

SAMPLE: <input type = "checkbox" name = "Sample" value = "X"> <BR><BR>

DATE:   <input type = "textbox" name = "SampleDate" onKeyPress = "autoCheck( );">




</form></body></html>

I thought it would piece together something like the above.

Any thoughts?
 
You don't want to just check for a keypress only, that's bad news. What if they are just tabbing thru the elements on the page, and tab past that textbox? Surely you don't want it to become checked in that instance. Use onkeyup and check to see if there is anything actually in the text field, if so, check the box. I wrote up a sample here. This will even uncheck the box if they enter something, and backspace it out if they decide they don't anything.
Code:
<html><body>

<script language = "javascript">
<!--
function autoCheck(obj) {
   if (obj.value.length == 0) {
      document.forms["drf"].elements["Sample"].checked = false;
   }
   else {
      document.forms["drf"].elements["Sample"].checked = true;
   }
}
//-->
</script>



<form name = "drf">

SAMPLE: <input type = "checkbox" name = "Sample" value = "X"> <BR><BR>

DATE:   <input type = "textbox" name = "SampleDate" onKeyUp = "autoCheck(this);">




</form></body></html>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Actually, one last thing.

'onKeyUp' does not work with my function. I used 'onChange'. Reason being- I'm using
Pop-up calendar. Pretty nice free code that lets you click a .gif and a calendar populates. You can navigate through that.

Other than that your code wored perfect.

 
glad it helped

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top