What is difference between prop and attr?

Answer Posted / chaitanya

attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements.

Attributes carry additional information about an HTML element and come in name="value" pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties.

attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state. Find out more here.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the basic need to start with jquery? : jquery mobile

445


What are your tools of choice to get the jquery job done?

477


Please tell us that is jquery knockkout intended to compete with jquery or prototype or work with it?

423


What is the name of jquery method used for an asynchronous http request?

486


What is jQuery CDN?

507






Explain why there are two different version of jquery library? : jquery mobile

445


Tell me can we have multiple document.ready() function on the same page? : jquery mobile

448


Explain the each() function?

485


What is the method used to define the specific character in place of $ sign?

568


How can we apply css in last child of parent using jquery library?

474


How we can remove an attribute of an html tag in jquery?

441


How to validate email using jquery?

461


Will Events Are Also Copied On Clone In Jquery?

476


Define "beforesend(xhr)" paremeter of jquery ajax method?

436


How do you disable or enable a form element?

451