4

Possible Duplicates:
.prop() vs .attr()

What is the differnece between doing

$('div').prop('title','whatever'); //set
$('div').prop('title'); //get

and

$('div').attr('title','whatever'); //set
$('div').attr('title'); //get

prop() seems to behave the same as attr() in the case of div attribute. I read the 1.6 documentation for this but i'm still a little confused.

THIS IS NOT EXACT DUPLICATE OF .prop() vs .attr(). This post doesn't answer the question. Please do not close it.

Community
  • 1
  • 1
Pinkie
  • 10,126
  • 22
  • 78
  • 124
  • @Frédéric That's already been closed itself... – lonesomeday May 03 '11 at 21:43
  • 1
    @Pinkie, your previous question was closed because it already was a duplicate. Posting the same question again is probably not the way to go. If the answers to the original question do not suit you, please expand your question to clarify exactly you're missing. – Frédéric Hamidi May 03 '11 at 21:44
  • @Frédéric Hmm, I didn't notice the user was the same. All the same, it's a valid question, and avowedly not a duplicate of `prop` vs `attr`. The first question should not have been closed. – lonesomeday May 03 '11 at 21:46
  • @Frédéric There is not duplicate to this question. Otherwise i woudln't whave asked it. You should pay more attention before voting to close. – Pinkie May 03 '11 at 21:51
  • @lonesomeday, @Pinkie, since `title` is both an HTML attribute and a DOM property, it makes sense for `prop()` to behave as `attr()` in that context. Answers to the original question already make that clear, so I fail to see why this isn't a dupe... – Frédéric Hamidi May 03 '11 at 21:52
  • @Frédéric "title is both an HTML attribute and a DOM property" wouldn't been an excellent answer just as @lonesomeda pointed in this answer. – Pinkie May 03 '11 at 21:57
  • @Pinkie, duly noted, but I really thought the answers to the original question made that obvious. My bad. – Frédéric Hamidi May 03 '11 at 22:06

1 Answers1

5

prop and attr do not always give different results. They only give different results when there is a difference between a property and an attribute.

For instance, with the checked attribute/property:

$(':checkbox').prop('checked'); // returns boolean true false
$(':checkbox').attr('checked'); // returns string "checked" or undefined

This is all well explained on prop vs attr.

However, with the title attribute/property, there is no difference. They are both strings, and setting one will set the other to that value.

Community
  • 1
  • 1
lonesomeday
  • 233,373
  • 50
  • 316
  • 318