Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Which is better prop() or attr()?

$(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style'); }) or in this case do they do the same thing? And if I do have to switch to using prop(), all the old attr() calls will break if i switch to1.6?

user-image
Question ajoutée par Muhammad Usman Usman , Software Enginner , Five Rivers Technologies
Date de publication: 2013/07/10
Utilisateur supprimé
par Utilisateur supprimé

so prop() is jquery1.6+ alternative for the .attr(), in terms of concept they do the same, but each is for a different version,we upgraded lately to jquery1.8 and found that it broke, we had to replace all of it's occurrences with the new jquery alternative, since .attr() is not supported any more.

Mohammad Arif
par Mohammad Arif , Principal Engineer UI , NatWest Group

The difference between attributes and properties can be important in specific situations.
Before jQuery1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.
As of jQuery1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method.
Prior to jQuery1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr.
These do not have corresponding attributes and are only properties.
According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false".
This is true of all boolean attributes.
Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property.
The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox.
The checked attribute value does not change with the state of the checkbox, while the checked property does.
Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property: if ( elem.checked ) if ( $(elem).prop("checked") ) if ( $(elem).is(":checked") ) Source - http://api.jquery.com/prop/

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

I found a very good reply over internet: http://stackoverflow.com/questions/5874652/prop-vs-attr

Munsif Mulla
par Munsif Mulla , Senior UI Engineer , FreeCharge

If you are trying to get the values then you should better go for "attr()" Even this can be used to set the values as well, Like if you want to set your values for style, then $('#selector').attr('style', { prop1 : 'value1', prop2 : 'value2'}); Hope it helps you..

Faizan Qureshi
par Faizan Qureshi , Principal Software Engineer , Naseeb Online Services

The link that Muhammad Majid Saleem provided explains a lot as far as the difference is concerned.
I'll just like to add one advise to help you out incase such functions are again replaced in the future. 
What i did was that i created my own functions that in functionality did the same thing as the original predefined functions do. 
For example for this attr function I created a new function that was some thing like this function _attr(o,a,v) { if(typeof(v) != 'undefined') $(o).attr(a,v); else $(o).attr(a); }   This way all that i had to do was to replace the code in this function and all occurances were fixed.
 

Muhammad Ahsan Mirza
par Muhammad Ahsan Mirza , Software Development Engineer , Remit Anywhere Solutions

.prop() as the name indicates refers to the properties while .attr() refers to an element's attributes.
Both of them can be used interchangeably for most of the cases.
As far as your question is concerned, you should go with .prop().

Fadi Alkhateeb
par Fadi Alkhateeb , Senior Front End Developer , NexTwo

.attr() and .prop() are same, but .attr() was valid and used until jquery1.6+ then prop() comes as an alternative.

More Questions Like This