Can I, a jQuery1.9+ software developer, "deprecate" the use of the attr()
method in my day-by-day work?
As showed in many questions,
- .prop() vs .attr() (main)
- jQuery attr vs. prop, there are a list of props?
- jQuery attr vs prop?
- Migrating jQuery 1.8.3 to 1.9.0 - Replacing deprecated .attr()
- ... etc. ...
there are a lot of confusion about "use attr or use prop?", and, by my (developer's) view point, for all uses of attr()
method, we can use prop
instead:
- backward-compatibility: coding new software not need this;
- performance: John says "Accessing properties through the .attr() method will be slightly slower than accessing them directly through .prop()";
- Change attribute value: all can be changed by the
prop(name,newvalue)
method. - Remove attribute: all can be removed by the
removeProp(name)
method. - Check HTML attribute values: browser use DOM, all HTML was converted to DOM, and, if DOM affected the
attr(name)
method also affected. About "strong type" of prop: it is better than "html string value" (ex. "checked" vs true). - Check if an attribute was defined in the "HTML original code" (supposing that method
attr
in your browser returns undefined if it is not)... Well, we need this in some piece of software? At forms, ".val() method is the recommended jQuery way to get or set the values of form" - Cross-browser consistency: both (not only attr) are consistent methods. (it is??).
So, at this time (2013), I not see a good reason to use attr
method when developing new jQuery code... But, well, this is, in other words, the question: There are a good reason to use attr
method in my day-by-day tasks?
attr
vsprop
most commom DOM-use?", or "What is the most frequent DOM-use?"... And, sorry, perhaps because of my "express-less", or bad english: only I posted here an direct answer for this kind of position. Most of the other answers are copies of these – Emulsoid