Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference betweeen prop and attr in jquery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 227
    Comment on it

    The attr() is a method which either returns or set selected elements values and attributes An attribute value must be a string whereas a property can be of any type. prop() methods gets the property value for the first element in the given matched set. A property is in the DOM while attribute is in the HTML that is parsed into DOM.

    <input blah="hello">
    

    Add a below code inside your script

    function myfunc()
    {
       return '<hr/>.prop: ' + $('input').prop('param') + '<br/>' +
              '.attr: ' + $('input').attr('param') + '<br/><hr/>';
    };
    
    var html = myfunc();
    
    $('input').prop('param', 'pear');
    $('input').attr('param', 'apple');
    
    html = html + myfunc();
    
    $('body').html(html);
    

    The output will come .

    prop: undefined
    .attr: hello
    

    and

    .prop: pear
    .attr: apple
    

    Another example is with the style parameter.

    <input style="font:arial;"/>
    
    .attr('style') -- returns inline styles for the matched element i.e. "font:arial;"
    .prop('style') -- returns an css style declaration.
    

 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: