Jump to content

Best way to get an element value - js + DOM


rv20

Recommended Posts

I am trying to work out if one of my checkboxes is checked or not, the CB is inside a form called "myform" if that makes a difference and the checkboxes ID = "check_terms" - oh and the function name is also check_terms so i didn't make a mistake.

 

so i tried,

 

function check_terms(){

if (document.check_terms.checked == false){
alert('test');
}

}

 

didn't seem to work after clickig submit, i then tried,

 

function check_terms(){

if (document.getElementById('check_terms').checked == false){
alert('test');
}

}

 

You may have to add a forms['myform'] or something in there somwhere i couldn't get it,

 

 

Should you use document.getelementbyID...... or just document."elementname"..... 

 

or is it window.document.elementname.....

 

confused!

Link to comment
https://forums.phpfreaks.com/topic/159400-best-way-to-get-an-element-value-js-dom/
Share on other sites

You should never use the same name for anything even if they are structurally different objects (e.g. a function and a field name). It will only cause confusion even if it does work.

 

In any event, the second block of code should work - at least it did for me when I tested it with a field and function named the same (both IE & FF).

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.