robert_gsfame Posted March 20, 2010 Share Posted March 20, 2010 How come that this code is not working? <script type="text/javascript"> function check() { if(document.getElementById('id1').checked == true) { alert("number 1 checked"); }else{ alert("number 1 not checked");} </script> and here is my html code <form name=form1> <input type ="checkbox" name="checkbox1" id="id1">Checkbox 1 <input type="button" name="button1" onclick="check()"> </form> Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted March 20, 2010 Author Share Posted March 20, 2010 sorry, i think the problem is that i have 2 form the first one is where the checkbox is and the other where the button is...but then how can i get the id of that checkbox document.form["form1"].getElementbyId("id1").checked==1 ?? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 21, 2010 Share Posted March 21, 2010 If you properly indent the code to show structure you probably would have noticed that the function doesn't having a closing curly bracket. function check() { if(document.getElementById('id1').checked == true) { alert("number 1 checked"); } else { alert("number 1 not checked"); } By the way, there's no reason to use "== true" int he coparison. When doing an IF condition, the whole point is to determine if the conditions equal true. So just testing "document.getElementById('id1').checked" would be more efficient. Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted March 21, 2010 Author Share Posted March 21, 2010 thx mjdamoto..you're right about the closing bracket! Thank you so much! I didn't notice about that... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.