Tutorial by Examples

When a single parameter is passed to the .attr() function it returns the value of passed attribute on the selected element. Syntax: $([selector]).attr([attribute name]); Example: HTML: <a href="/home">Home</a> jQuery: $('a').attr('href'); Fetching data attributes: jQue...
If you want to add an attribute to some element you can use the attr(attributeName, attributeValue) function. For example: $('a').attr('title', 'Click me'); This example will add mouseover text "Click me" to all links on the page. The same function is used to change attributes' values...
To remove an attribute from an element you can use the function .removeAttr(attributeName). For example: $('#home').removeAttr('title'); This will remove title attribute from the element with ID home.
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: $('#tosAcc...

Page 1 of 1