- Posted On 21 September 2014
- In Programming
This post contains information about Attr() and Prop() method available in jQuery. Most of the developers are confused between Attr() and Prop() method with respective to when to use which and most of the time they use it without knowing the basic difference so I decided to have some post on it.
Let’s start step by step.
What is Attribute?
Attribute in html provides some additional information about that element. All attributes are always specified in the start tag and comes in name/value pair.
<p title=”About us”>This is some information about us….</p>
If you look at above example “title” is the attribute of “p” element. You can provide custom attributes too.
What is Property?
Now let’s understand what property in HTML is. Property is named characteristic of an element in the internal model of an HTML document i.e. DOM.
<input type="text" value="abc" id=”txtInfo”>
Difference between Attribute and Property in HTML.
Now let’s discuss the difference between Attribute and Property. As said above attributes are written in start tag of an element and properties are represented in DOM.
<input type=”text” id=”txtInfo” value=”Type Here:”/>
Above mark up will render a textbox with a value “Type here:” within it.
At this stage txtInfo.Value and txtInfo.getAttribute('value') will return the same value i.e. “Type Here:”.
And now if you have typed “abc” in it.
Now txtInfo.getAttribute('value') will return “Typed Here:” and txtInfo.Value will return “abc”.
When to use Attr() and Prop() in jQuery
I hope till now you are already cleared about the basic difference between attribute and property and same concept gets applied with Attr() and Prop().
Attr() returns the initial value of some attribute of an element
Prop() returns the current and latest(if changed) value of the property of an element.
Confusion between these two had been started since jQuery introduced their version 1.6 with where they have changed the behavior of attr() method which previously considering properties too while returning the value of attribute which is creating inconsistency in the behavior. Again in version 1.6.1 they have roll backed the behavior of attr() to its old behavior(not exactly same though).
So as conclusion when you want to change the values and want to write some logic on the basis of current values of property such as “checked” property of “checkbox” prefer to use prop() and of initial values are in consideration prefer to use attr().
You can check below live fiddle in which I have checkboxes and I have attempted to toggle its check with prop() and attr() and if you check by clicking on the buttons. “Toggle state(attr)” toggles state for once and then it doesn’t work on the other hand prop() always toggle the state.
I hope this post has benefited you to clear the difference between attr() and prop(). Do share your comments and share this with your friends. Thanks.
Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio
Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.
Swagger - Powerful Opensource Framework for RESTful API
With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.
Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
How to call click or any event only once in jQuery
Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.