In this case you need prop()
rather than attr()
,
replacing calls to attr()
with prop()
in your code will generally work.
From http://blog.jquery.com/2011/05/10/jquery-1-6-1-rc-1-released/
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr()
method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop()
method provides a way to explicitly retrieve property values, while .attr()
retrieves attributes.
elem.checked
==== true (Boolean)
Will change with checkbox state
$(elem).prop("checked")
==== true (Boolean)
Will change with checkbox state
elem.getAttribute("checked")
====="checked" (String)
Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6)
====="checked" (String)
Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6.1+)
========"checked" (String)
Will change with checkbox state
$(elem).attr("checked") (pre-1.6)
=======true (Boolean)
Changed with checkbox state
Also this url will help you more about your queries .prop() vs .attr()
A difference of /is-checked-vs-attr-checked-checked/7
on http://jsperf.com/is-checked-vs-attr-checked-checked/7
Also to understand The elements atttribute and properties
refer http://christierney.com/2011/05/06/understanding-jquery-1-6s-dom-attribute-and-properties/
http://jsperf.com/is-checked-vs-attr-checked-checked/7