Jump to content

Function When Checkbox Is Checked Or Unchecked?


astralaaron

Recommended Posts

Your question could be more clear. Maybe you're asking about the onchange property? You can assign an event handler function to that and it will fire when the box is checked or unchecked. The function itself can get the checked state in this.checked, which will return true or false.

Link to comment
Share on other sites

Your question could be more clear. Maybe you're asking about the onchange property? You can assign an event handler function to that and it will fire when the box is checked or unchecked. The function itself can get the checked state in this.checked, which will return true or false.
yeah that sounds right, I meant I want to have a function execute when you check the box or uncheck.thanks
Link to comment
Share on other sites

having some trouble with this, it turns the color yellow but when I uncheck it, it doesnt change to red..its confusing too, I thought when I checked it, it would change red. then uncheck would change to yellow. function lightup(id) { elm = document.getElementById(id); if(document.delr.id.checked == true) { elm.bgColor = "red"; } else { elm.bgColor = "yellow"; } }<form name="delr" ><tr id="x1"><td><input type="checkbox" name="x1" onchange="lightup('x1');" /></td></tr>*EDIT: when I add alert(document.delr.id.checked); to the function it alerts: undefined..

Link to comment
Share on other sites

I almost never refer to form elements using dot notation. It has history, but it's not standard. Better to use an id and then document.getElementById instead. Some browsers (like IE) can get confused if elements have the same name and id. I forget the different ways it can object, or if this is one of them, but it's best to play it safe.Did you print the actual structure of this thing? With the form tag coming inside a table, but before a table row element? That can only lead to trouble. Nothing is permitted to exist between rows.With every change you make, alert the reference to your input element until you see that it's valid. Then test the rest of the code.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...