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
Share on other sites

Second version should work (if you got a <input type='checkbox' id='check_terms' /> on the same page)

document.getElementById( 'id' ); is correct.

 

Think the first version indeed needs a form reference in there somewhere.

 

Link to comment
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.