As far as I am aware, the .prop() function can do everything the .attr() function can do but in a generally safer and simpler way.
For example:
- If I want to get the default state of a checkbox in the HTML, I can do
$('myCheckbox').prop('defaultChecked')(instead of$('myCheckbox').attr('checked')). This actually seems safer than using.attr('checked'), as the attribute can lose it's value if the checkbox is dynamically changed, while.prop('defaultValue')maintains the value (e.g. http://jsfiddle.net/p1Lrgwnb/1/) - Even though I often see
.attr()used consistently with values such asidandnameon StackOverflow examples,.prop()works fine with those as well. I am unaware of any reason.attr()seems to be preferred for these values other than traditional conventions and habits.
Is there ever a use case where I would need to use .attr() or that the .prop() function would not give me the information I needed?
EDIT: This question has nothing to do with what is the difference between .prop() and .attr(). I've studied those questions on StackOverflow in depth, including the one linked below (stackoverflow.com/questions/5874652/prop-vs-attr ). From my question, it is clear I understand fully the difference between the two, probably better than most. My question is are there any circumstances I must use .attr(), which is a completely different question from .prop() vs .attr().