Attributes in jQuery
Some of the most basic components, we can manipulate DOM elements, are properties and attributes assigned to those elements.
Some of the most basic components, we can manipulate DOM elements, are properties and attributes assigned to those elements.
Most of these attributes are available through JavaScript as DOM node attributes. Some of the more common attributes are:
- className
- tagName
- id
- href
- title
- rel
- src
Consider the following HTML code fragment for an image element:
id = "imageid" src = "image.gif" alt = "Image" class = "myclass" title = "This is an image" />
In marking this element, the tag name is img, and the markup for id, src, alt, class, and title represents the properties of the element, each with a name and a value.
jQuery provides us with methods to manipulate element properties easily and help us access these elements so that we can change their properties.
Get attribute values in jQuery
The attr () method can be used to get the value of an attribute from the first element in the matched set or set the attribute values on the matched elements.
For example
The following is a simple example that takes the title attribute of the tag and sets the value
The jQuery Example