Both perform the same functions.
The difference is that the method .prop()
is in the DOM
and the method .attr()
is in the HTML
.
An example, clearer and generic, is when you try to access an attribute that does not exist in an element, you can access this attribute in the DOM (using the .prop()
), however, this attribute is always empty by default. Accessed by the method .attr()
, returns as undefined
.
Example:
v = $("#foo").prop("class");
console.debug("prop retorna vazio: "+v);
v = $("#foo").attr("class");
console.debug("attr retorna indefinido: "+v);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo">bar</div>
See the result for the console in Veloper tools. On Chrome, press Ctrl+Shift+I and click on the "console".
It will result in:
prop retorna vazio:
attr retorna indefinido: undefined
Take a look at these answers in English and you have several examples http://stackoverflow.com/questions/5874652/prop-vs-attr
– Gabriel Rodrigues
great Gabriel! I’ll see
– DiChrist