Both have the same functions.
The difference is that the .prop()
method is in DOM
and the .attr()
method is in HTML
.
An example, more clear and generic, is when you try to access a non-existent attribute in an element, you can access that attribute in the DOM (using .prop()
), however, this attribute is always empty by default. If it is accessed by the .attr()
method, it 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><divid="foo">bar</div>
See the result for the console in developer tools. In chrome, press Ctrl + Shift + I and click on the "console" tab.
It will result in this:
prop retorna vazio:
attr retorna indefinido: undefined