code
Attributes

We can manipulate attributes assigned to HTML elements easily through jQuery.
href, src, id, class, style are all examples of HTML attributes.

The attr() method is used for getting the value of an attribute.
For Example:
HTML: 

<a href="www.sololearn.com">
   Click here
</a> 

JavaScript

$(function() {
  var val = $("a").attr("href");
  alert(val);
});
// alerts "www.sololearn.com"

NOTE!
In the code above we selected and alerted the value of the href attribute of the element.


Attributes
 

The attr() method also allows us to set a value for an attribute by specifying it as the second parameter.
For example:

$(function() {
  $("a").attr("href", "http://www.jquery.com");
}); 

 

NOTE!
This will change the href attribute of the element to the provided value.


BACK NEXT