Monday, April 29, 2013

jQuery - when to use attr() ?? when to use prop() ???

with effect from jQuery 1.6 a new method has been introduced to change or retrieve values of attributes (or properties).

So what is the big deal here you might ask. the problem is that new comers doesn't understand the difference between the attr() method which has been there and the new method prop() which introduced lately.

In a HTML element there is two kind of  things we can see.
  1. Attributes
  2. Properties
I will explain this by an example. Lets take following HTML input tag for an instance.

 <input id="q" value="" class="button-gray" name="_3_keywords" readonly>  

In the above example  id, value, name are belong to Attribute family while readonly is a property. So when to use attr() method or when to use prop() method is depend on whether you are dealing with an attribute or a property.

For example lets say you want to change the class attribute of above INPUT tag. Here you have to use  attr() method like below.

 $("#q").attr("class", "newclass")  

But lets say if you want to make the above INPUT tag readable (it is readonly originally according to the code above). So here you have to use prop() from  jQuery 1.6 onwards like below.

 $("#q").prop("readonly", false)  

Yeah you might noticed the change. Before jQuery 1.6 you have to use attr() method like below. 

 $("#q").attr("readonly", "readonly")  

But with new  prop() you can save life by just giving a boolean as the second attribute.

 


21 comments:

  1. 1) The jQuery prop() grabs the specified DOM property whereas attr() grabs the specified HTML attribute.
    2) The jQuery prop() method returns boolean value for selected, checked, readOnly, disabled and so on while attr() returns a defined string.

    For details information look here:
    http://www.namasteui.com/difference-between-prop-and-attr/

    ReplyDelete
  2. You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  3. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  4. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    best blockchain online training

    ReplyDelete

  5. This post is so interactive and informative.keep update more information...
    Advantages of Cyber Security
    Importance of Cyber Security

    ReplyDelete
  6. I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,..
    data analytics courses in hyderabad with placements

    ReplyDelete
  7. This post is so interactive and informative.keep update more information...
    Android Training in Tambaram
    Android Training in Chennai

    ReplyDelete