Can anyone tell me how you check or uncheck a checkbox using javascript?
Printable View
Can anyone tell me how you check or uncheck a checkbox using javascript?
Replace [form name] and [checkbox name] with the names of your form and checkbox, respectively :). Set it to false to uncheck the box.Code:document.[form name].[checkbox name].checked = true;
hey vinnie, along these same lines....how would you refer to this element using getobjectbyid[]?
ta very much
Not sure what you mean. Are you talking about getElementById? If you want full DOM compliance, then you can have something like this:Quote:
Originally Posted by Sketch
JS
HTMLCode:document.getElementById("myCheckbox").setAttribute("checked", "checked");
If you're talking about something else, let me know and I'll try to work up a solution :).Code:<label for="myCheckbox">Restart server?</label> <input type="checkbox" name="myCheckbox" id="myCheckbox" />
No that's what I meant. It just seems like using getelementbyid always coughs for form fields and I haven't been able to figure it all out yet.
Form fields require a "name" attribute per the HTTP spec, and a lot of people forget to use IDs as well, which are required for <label> tags to work properly, as well as for scripts (scripts using getElementById() and getElementsByTagName() at least) to work properly. Another collection like getFormFieldsByName() would have been nice in the DOM to avoid this confusion and replace the old document.forms[].elements[] collections.Quote:
Originally Posted by Sketch
True vinnie, but I still prefer the collections to the literal properties
this
document.forms['form1'].elements['input1']
rather than this
document.form1.input1
Sure, it's more typing, but FAR less problematic - and I'm not really that lazy to type a dozen extra characters
I agree; I was just giving the "quick-fix" version ;). The problem with the document.forms[] collection (or the shortened document.[form name] sytnax) as I understand it is that when XHTML is sent to the browser as XML the scripts using those collections will fail though.Quote:
Originally Posted by beetle