(1) Except, you know, ie 7 and ie 8. So... half the internet.
(1) Except, you know, ie 7 and ie 8. So... half the internet.
List compiled (partially) from http://stackoverflow.com/questions/706384/boolean-html-attri...
Really, most people reading the changelog care about "how do I make my existing code work with this"--and the details listed are conceptual, but not all practically relevant.
$.fn.setAttr = function(attr, bool) {
if(bool)
this.attr(attr, attr);
else
this.removeAttr(attr);
return this;
}
$.fn.disabled = function(bool) {
return this.setAttr('disabled', bool);
}
$.fn.checked = function(bool) {
return this.setAttr('checked', bool);
}
I assume I should change all attr to prop?I think you should add two more helpers (like .val):
.checked() and .disabled() - with no parameters they return a boolean, and with a parameter you can pass in true/false to set the property.
Those two are probably the most commonly used properties and if you had them most people could probably ignore attr vs prop.
The bug and commit are as follows: http://bugs.jquery.com/ticket/9079 https://github.com/jquery/jquery/commit/a9d9f8c5422f171d9c45...
You can start using this code right now with the following version of jQuery: http://code.jquery.com/jquery-git.js
Hope this helps!
Eg
$(optionElement)
.prop('selected', true)
.attr('selected'); // undefined in 1.6, "selected" in 1.6.1 <input type="checkbox" id="c"/>
$("#c").attr("checked") => false // 1.5.2
$("#c").attr("checked") => undefined // 1.6
$("#c").attr("checked") => undefined // 1.6.1
The user then checks the checkbox OR we set the check by doing .prop("checked", true) OR we set the check by doing [0].checked = true, the result would be: $("#c").attr("checked") => true // 1.5.2
$("#c").attr("checked") => undefined // 1.6
$("#c").attr("checked") => "checked" // 1.6.1
Note that if we had done .attr("checked", "checked") OR .attr("checked", true) to set the checked property then .attr("checked") would've returned "checked" in both 1.6 and 1.6.1.In short: In jQuery 1.6.1 .attr(BooleanAttribute) now returns either the name of the attribute (if true) or undefined (if false). Which is consistent with the rest of the API and with the DOM itself.
I should note that all of this is a moot point if you're using .prop("checked") - that will just return true/false consistently based upon the current checked state of the checkbox.
The doc reads: "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() only retrieves attributes.
For example, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />..."
The docs then go into the example on this thread.
Personally, I think that if this is the only specific situation we can think of, it probably wasn't worth introducing a method that parallels attr. As a higher-level library, jQuery ideally will do the right thing without the calling code thinking too hard about exactly what mechanism it's using to do it. (It does this with cross-browser issues, for example.) This was the case already with .attr("checked")...
So if I'm approaching jQuery for the first time, do I use prop or attr, and why?
I don't see how prop() is any different than just using data().