attr()
gets/sets the HTML attribute using the DOM functions getAttribute()
and setAttribute()
. prop()
works by setting the DOM property without changing the attribute. In many cases the two are interchangeable, but occasionally one is needed over the other.
To set a checkbox as checked:
$('#tosAccept').prop('checked', true); // using attr() won't work properly here
To remove a property you can use the removeProp()
method. Similarly removeAttr()
removes attributes.