4

I'm having an issue verifying if a checkbox is checked using jquery on Internet Explorer. This is the code I'm using:

if ($('#chkjq_1').attr('checked') == true)

It works fine on Firefox or Chrome, but on Internet Explorer 7, the condition is always false because the browser sets the property this way:

<input id="chkjq_1" type="checkbox" CHECKED/> IE7

And the right way is the following: (Firefox, Chrome):

<input id="chkjq_1" type="checkbox" checked="checked"/> FF, Chrome, etc

What should I do to avoid this issue on Internet Explorer 7; is there a generic way in jquery to solve this?

Thanks in advance.

lidermin
  • 2,782
  • 11
  • 43
  • 48
  • what version of jQuery are you using? – Samuel Mar 26 '10 at 17:31
  • 1
    See http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery – Mayo Mar 26 '10 at 17:32
  • What is responsible for generating your input tags? Is this an ASP.NET server control or something similar? Best to focus on why the tag is being rendered incorrectly and see if you can address that; then the jQuery should just work. – Noah Heldman Mar 26 '10 at 17:33
  • lidermin, you mean jquery UI 1.7.2, which needs jquery 1.3+ to run; Anyway, the answers you have are working with any jquery 1.0+ – leoinfo Mar 26 '10 at 18:19
  • Sorry, You are right, I meant jquery 1.3.2. – lidermin Mar 26 '10 at 19:29

3 Answers3

17

Try this:

if ($('#chkjq_1').is(':checked'))
{
  // more code
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • it works for me also . in case of my 'CHECKED' is not working in IE9 and i change it to 'checked' and works . – Asraful Dec 03 '12 at 09:19
3

The only thing I could get to work consistently was to test like this:

if ($('#chkjq_1').is('[CHECKED]'))

Using all caps on the selector like IE renders it when you open Developer Tools. Works across browsers so far as I can tell. .is(':checked') wouldn't match for me (using JQuery 1.7.1). Tested using IE 7 and IE 8 in multiple modes. Other browsers (Firefox, Safari tested) seem not to care.

S. Rooks
  • 41
  • 1
1

"change" event of checkbox in IE is triggered weirdly.

This can be solved as:- 1.) bind the onclick event of input checkbox to required target function to be fired 2.) in the target function, check the status of the checkbox by checking the "checked" attribute via-

if ($('#chkBox').attr("checked") == "checked"){
      // do something ----or .attr("checked", "checked"); ---for another checkbox
}else{
      // do something else ----or .removeAttr("checked"); ---for another checkbox
}