5

I saw a comment saying

It'd be better to set the "title" property with .prop() instead of .attr() if you're using at least version 1.6 of jQuery.

Can someone explain why this is?

Joe Flynn
  • 6,908
  • 6
  • 31
  • 44
  • "The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes." – Pim Oct 09 '12 at 17:37
  • 1
    See http://stackoverflow.com/a/5876747/545858 – Keethanjan Oct 09 '12 at 17:38
  • I think in this case `.prop` vs `.attr` doesn't matter due to how the title property and attribute behaves. If you update one, the other gets updated. In the 1.6.1 release blog post (http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/), it was listed in the table as `.attr()` being the preferred method for title, but no explanation as to why was given. – Kevin B Oct 09 '12 at 18:00

2 Answers2

6

I guess you're familiar with the differences between prop and attr.

As the title attribute is reflected by the title property, there is actually no difference.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • If I set with .attr will it be visible when I check the HTML code using firebug or another debugger. If so I see this as an advantage. –  Oct 09 '12 at 17:44
  • As I said, they will be reflected - if you set the `.title` property, the DOM (which you inspect with Firebug) should change as well - the inspector's view might need a reload – Bergi Oct 09 '12 at 17:47
0

It depends.

prop is used for getting properties of HTML objects

attr is used for getting atttributes.

The prop documentation from jQuery explains it quite well.

tkone
  • 22,092
  • 5
  • 54
  • 78
  • I realize the above but I saw one of the posters here say it was better to use .prop for the title. Just wondering why that was said. –  Oct 09 '12 at 17:45