Do these two jQuery functions do the exact same thing? Or is there some difference between them.
It is reported that the
.prop() function came from jQuery 1.6
.attr() come to replace
.attr() ? Or are the two present (and used) today in the library having the same features for those who want to use them?
Both perform the same functions.
The difference is that the
.prop() method is in the
DOM and the
.attr() method is in the
A clearer and more generic example is when you try to access an attribute that does not exist on an element, you can access that attribute in the DOM (using
.prop() ), however, this attribute is always empty by default. If accessed via the
.attr() method, it returns as
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>
View the result via the developer tools console. In chrome, press Ctrl+Shift+I and click on the "console" tab.
It will result in this:
prop retorna vazio: attr retorna indefinido: undefined