jQuery: .attr() vs .prop()
In jQuery, both .prop() and .attr() methods are used to manipulate properties and attributes of HTML elements, but they serve different purposes and have some important distinctions:
.prop() Method
- The .prop() method is primarily used for getting or setting properties of DOM elements, such as boolean attributes (e.g., checked, disabled) or properties (e.g., selectedIndex, nodeName).
- It is recommended for working with properties that represent the current state of an element, especially when dealing with form elements and their states (checked, selected, disabled).
- When used with properties, it typically returns the current state as a boolean value (true or false) or sets the state by passing true or false as an argument.
.attr() Method
- The .attr() method is used for getting or setting attributes of HTML elements, such as standard HTML attributes (e.g., src, href, class) or custom attributes.
- It is suitable for working with attributes that are part of the element's initial state and remain constant throughout the element's lifecycle.
- When used with attributes, it usually returns the current attribute value or sets a new value for the attribute.
Working with Checked State (Property) vs. Data Attribute (Attribute)
<input type="checkbox" id="myCheckbox" checked="checked" data-info="Some data" />
// Using .prop() to get/set the checked state (property)
var isChecked = $("#myCheckbox").prop("checked"); // Returns true
$("#myCheckbox").prop("checked", false); // Unchecks the checkbox
// Using .attr() to get/set the data attribute
var info = $("#myCheckbox").attr("data-info"); // Returns "Some data"
$("#myCheckbox").attr("data-info", "New data"); // Updates the data attribute
In this example, .prop() is used for the checked state property, and .attr() is used for the data attribute.
Working with Standard Attributes
<a href="https://www.example.com" id="myLink">Visit Example</a>
// Using .attr() to get/set the href attribute
var linkHref = $("#myLink").attr("href"); // Returns "https://www.example.com"
$("#myLink").attr("href", "https://www.newlink.com"); // Updates the href attribute
In this case, .attr() is used to work with the standard HTML href attribute.
Conclusion
The choice between .prop() and .attr() depends on whether you are dealing with properties that represent the current state of elements (use .prop()) or attributes that define the element's initial characteristics (use .attr()). Understanding this difference is crucial for proper manipulation of elements in jQuery.
Related Topics
- jQuery Interview Questions (Part-2)
- jQuery Interview Questions (Part-3)
- Is jQuery a programming language?
- Why do we need to go for JQuery?
- How to check jQuery version?
- How to multiple version of jQuery?
- What is jQuery CDN?
- Advantages of minified version of JQuery
- How do I check if the DOM is ready?
- How to Use the jQuery load() Method
- Difference between document.ready() and body onload()?
- Is jQuery is a replacement of JavaScript?
- JQuery or JavaScript which is quicker in execution?
- What is the use of param() method in jquery
- How to work with jQuery parent(), children() and siblings()?
- Difference between parent() and parents() in jQuery?
- What does jQuery data() function do?
- How do you check if an element exists or not in jQuery?
- How do I check if an HTML element is empty using jQuery?
- How to run an event handler only once in jQuery?
- How to Disable or Enable a Form Element Using jQuery
- Hide and show image on button click using jQuery
- How do I check if an element is hidden in jQuery?
- Difference between return false; and e.preventDefault()
- What is each() function in jQuery? How do you use it?
- Which one is more efficient, document.getElementbyId( "myId") or $("#myId)?
- What is the difference between $.map and $.grep in jQuery
- What is the use of serialize method in jQuery
- What is the use of clone method in jQuery?
- What is event.PreventDefault in jQuery?
- Difference between event.PreventDefault and event.stopPropagation?
- What are deferred and promise object in jQuery?
- What are source maps in jQuery?
- What does the jQuery migrate function do?
- Differences Between jQuery .bind() and .live()?
- How can you delay document.ready until a variable is set?
- How to disable cut,copy and paste in TextBox using jQuery?
- How to prevent Right Click option using jquery?
- How does the jQuery pushStack function work?
- Why use jQuery filter() Methods?
- Difference between find() and closest() in jquery?
- How To Use Ajax In Jquery?
- How to multiple AJAX requests be run simultaneously in jQuery?
- Can we call C# code behind using jQuery?
- How to include jQuery in ASP.Net project?
- Need to add jQuery file in both Master and Content page?
- Uncaught TypeError: $(…).modal is not a function jquery
- How to check whether a checkbox is checked in jQuery?
- Uncaught ReferenceError: $ is not defined
- How to Convert JSON Date to JavaScript/jQuery date