Krishna Kant Sharma
Krishna Kant Sharma

Reputation: 1587

how to check if checkbox is checked or not in IE 6

I am frustrated with the most idiotic browser of all time, for which, Bill Gates must be hanged I think.

I just want to check whether a checkbox is checked or not.

Somehow

 cbox.checked

property is always false. What other thing I can do. I saw all the other similar questions, but nothing is working with this stupid IE.

EDIT

I forgot to mention something that may be relevent. Html is like:

<input type='hidden' name='terms' value='0' /> 
<input type='checkbox' name='terms' id='terms' value='1' /> 

Hidden field is attached with it, because I am using Zend Form, and it always attaches a hidden field with every checkbox.

I am using protoype.js thats why I cannot use jQuery. I am checking that its check or not, in onsubmit event of the form. I guess somehow hidden field with the same name is tripping IE6

Upvotes: 3

Views: 2819

Answers (3)

Tim Down
Tim Down

Reputation: 324627

Now you've posted your HTML your problem is clear: you've got two form elements with name 'terms' and IE 6 is finding the wrong one, because it has a broken implementation of document.getElementById that uses the name attribute of form elements as the id. The solution is to ensure you don't have form elements with the same name as any element you wish to refer to by id, and avoid using the same name for unrelated form elements.

Note that the problem will also exist in IE 7, according to this blog post.

Upvotes: 2

Psytronic
Psytronic

Reputation: 6113

I have to agree, IE 6 is a pain sometimes, and unfortunately we have to cater to that minority who still haven't upgraded. But this works for me within IE6

EDIT: After reading Tims Post have updated function reflect this. Tested and works in IE6.

       function chk(){
        inps = document.getElementsByTagName("INPUT");
        for(i = 0; i < inps.length; i++){
         if(inps[i].name == "terms" && inps[i].type == "checkbox"){
          alert(inps[i].checked);
         }
        }
       }

Upvotes: 1

Stefan Kendall
Stefan Kendall

Reputation: 67862

$('#myElement').is(":checked")

Ignore the IE6 nonsense, and just use jQuery.

http://www.jquery.com

Upvotes: 5

Related Questions