Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


attr() v/s prop() in jQuery – Know the difference and when to use which

Share with WhatsApp

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”>

In above example, if you wrote a javascript code like txtInfo.value will return the value of textbox i.e. “abc”.

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.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.

Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.

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.

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.

Speed up coding in Visual Studio with code snippets & samples at your fingertips

Know how you can speed up coding in Visual Studio with Bing Developer Assistant by having millions of code snippets and sample projects at fingertips.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!