After digging the jQuery sources I still can’t understand why the code like:
$("div").attr("someAttr", undefined);
returns jQuery object but not an attribute value, despite the fact that the second value argument is set as undefined.
JSFiddle: http://jsfiddle.net/5jn5D/.
This is, because
.attr("someAttr", undefined)(with 2 arguments) is a setter method, and thus returns the object for sake of chainability. Setting the argument toundefinedor any other arbitrary value always yields the same effect. Why should a value be returned, if you know what the value is?If you use
attras getter method (with only 1 argument).attr("someAttr")it returns a string with the attribute value.Line 2003 in the source explicitely checks for the length of the arguments:
with the fifth argument being the
chainableswitch, returning true forarguments > 1.