Please take time to read the question in full before assuming that it's a duplicate of .prop() vs .attr()
I've read .prop() vs .attr() and a handful of the duplicates and related non-duplicates. My question is: when is it appropriate to pass values to .attr() when dealing with HTML and the DOM?
Of course the simple answer is "why, whenever you wish to set or modify the value of an attribute - otherwise (and usually) use .prop()."
But what I really want to know is when should you wish to. In the jQuery docs for the method, I see examples of setting things like id
, src
alt
, and title
on img
elements. But the docs don't seem to give any indication of why you may want to do so, over using .prop() to set these particular attributes.
What I'm after is a guiding principle of when to use .attr() to set things rather than .prop().
Related note: the particular case I'm working on is what's the proper way to change the action on a form, but really I want the perspective and understanding behind the decision.
edit To clarify, I am not looking for:
- the differences between properties and attributes
- the differences between .attr() and .prop()
- the differences between using .attr() and .prop() to get values for either attributes or properties
Otherwise, we could just mark this as a duplicate and call it a day :) No offense intended whatsoever to what's been offered so far - just wanted to make it clear that I'm looking for info on what determines when it's right to use .attr() to set the attribute value, which info I did not find on the venerable .prop() vs .attr() (though if it's there and I missed it, let's definitely mark this as a duplicate!) Thanks!