lidermin
lidermin

Reputation: 2832

jquery checked checkbox IE problem

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.

Upvotes: 4

Views: 29107

Answers (3)

user1353316
user1353316

Reputation: 11

"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
}

Upvotes: 1

S. Rooks
S. Rooks

Reputation: 41

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.

Upvotes: 3

Sarfraz
Sarfraz

Reputation: 382696

Try this:

if ($('#chkjq_1').is(':checked'))
{
  // more code
}

Upvotes: 17

Related Questions