.prop() vs .attr() in jquery
The .prop()
method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location
). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr()
method.
Below is a list of some attributes and properties and which method should normally be used when getting or setting them. This is the preferred usage, but the .attr()
method will work in all cases.
Attribute/Property | .attr() | .prop() |
---|---|---|
accesskey | Yes | |
align | Yes | |
async | Yes | Yes |
autofocus | Yes | Yes |
checked | Yes | Yes |
class | Yes | |
contenteditable | Yes | |
draggable | Yes | |
href | Yes | |
id | Yes | |
label | Yes | |
location ( i.e. window.location ) | Yes | Yes |
multiple | Yes | Yes |
readOnly | Yes | Yes |
rel | Yes | |
selected | Yes | Yes |
src | Yes | |
tabindex | Yes | |
title | Yes | |
type | Yes | |
width ( if needed over .width() ) | Yes |
Reference Url: http://blog.jquery.com/2011/05/10/jquery-1-6-1-rc-1-released/
-
CreatedJan 18, 2020
-
UpdatedOct 03, 2020
-
Views2,133
Related Articles
How to get the selected text of a dropdown item using jQuery?
How to get the selected value of a dropdown item using jQuery?
How do I check whether a checkbox checked in jQuery?
Setting “checked” for a checkbox in jQuery?
How to check if an element exists in jQuery?
Set DropDownList value based on text/value in jQuery
Get selected text/value from DropDownList (or) Select in jQuery
Get selected index from DropDownList (or) Select in jQuery
Clear items from DropDownList (or) Select in jQuery.
How to replace string in JQuery?