jQuery prop() vs. attr() 

Joined:
02/21/2009
Posts:
172

April 21, 2014 15:17:45    Last update: April 21, 2014 15:17:45
The .prop() method gets or sets the property value of the first element in the matched set. The .attr() method does the same to an attribute.

So what is an attribute and what is a property? Roughly speaking, an attribute is something you specify in the HTML markup. A property is a property of the element, but its initial value may be derived from the value of an attribute. For example, selectedIndex is a property of <select> that does not have a corresponding attribute in HTML markup. The <input> element can have a value attribute that specifies the initial value of a text input, but its value property reflects the current text entered into the text input. Setting the value property does not affect the value attribute:
// set the attribute to "Hello"
$('input:first').attr('value', 'Hello');

// set the property to "Bye"
$('input:first).prop('value', 'Bye');

// the attribute is still "Hello"
$('input:first').attr('value');


For more details: .prop() | jQuery API Documentation
Share |
| Comment  | Tags