Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between attr() and prop() in JQuery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 276
    Comment on it

    Many of us uses JQuery.attr() and JQuery.prop() and get the same result, as there is a small difference between the two.

    Lets discuss it one by one.
    JQuery.attr() :
    It generates the value of an attribute for the first element in the set of matched elements.

    JQuery.prop() :
    It generates the value of a property for the first element in the set of matched elements.

    Let make it more clear with an example:

    <input type="text" id="name" value="findnerd" />
    

    It has three attributes.
    1. type
    2. id
    3. value

    In this, attr() gives you the value of an element as it was defined in the HTML on page load.
    But prop() always keeps the current state value.
    That is why, It is always recommended to use prop() over attr() to get the values of elements as the element can be modified via JavaScript/jQuery in the browser at runtime.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: