Hacker News new | past | comments | ask | show | jobs | submit login

I don't think it's clear at all what the purpose of each is, especially if you were coming into jQuery fresh. I would have interpreted .prop as something more like .data.

The doc reads: "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() only retrieves attributes.

For example, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />..."

The docs then go into the example on this thread.

Personally, I think that if this is the only specific situation we can think of, it probably wasn't worth introducing a method that parallels attr. As a higher-level library, jQuery ideally will do the right thing without the calling code thinking too hard about exactly what mechanism it's using to do it. (It does this with cross-browser issues, for example.) This was the case already with .attr("checked")...

So if I'm approaching jQuery for the first time, do I use prop or attr, and why?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact