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)