Search within TutorialsPark

Jquery Attributes and Properties


Learn to use jQuery to set the values for attributes and properties of the elements.


jQuery Manipulation: Working with Attributes and Properties of Elements

jQuery provides several methods to set the values for properties and attributes of jQuery object.


Table : Methods for Working Elements

Method Description
attr(name) To remove elements from the DOM. The data associated with the elements is preserved.
attr(name, value) To remove all of the child nodes from each element in the jQuery object.
attr(map) To remove elements from the DOM.As the elements are removed, the data associated with the element is destroyed.
attr(name, function) To remove the parent of each of the element in the jQuery Object.
removeAttr(name)
removeAttr(name[])
To remove the parent of each of the element in the jQuery Object.
prop(name, value)
prop(map)
To remove the parent of each of the element in the jQuery Object.
prop(name, function) To remove the parent of each of the element in the jQuery Object.
removeProp(name) To remove the parent of each of the element in the jQuery Object.

Reading the Value of an Attribute using attr() Method

The jQuery Method attr() is used to set one or more attributes for the set of matched elements.

When you call the attr() method with a single argument, the jQuery returns the value of the specified attribute from the first element in the selection

Example: Reading the Value of an Attribute using attr() Method

Give it a TRY! » Note:The attribute of the image element is fetched using this method


Read attributes for Multiple Objects using each() and attr() Method

The jQuery attr() method can be combined with each() method to read values for all elements

Example: Read attributes for Multiple Objects using each() and attr() Method

Give it a TRY! » Note:If you need to reinsert an element you can do it with detach()


Setting an Attribute Value using attr(name, value) method

The jQuery method of attr(name, value) is used to set an attribute value, the change is applied to all of the elements in the jQuery object.

Example: Setting an Attribute using attr(name, value) method

Give it a TRY! » Note:The method attr() returns a jQuery object, meaning you can perform a method chaining


Setting Multiple Attributes using Map object

The jQuery method of attr(name, value) is used to set multiple attributes in a single method call by passing objects to attr() method.

The properties of this object are interpreted as the attribute names, and the property values will be used as the attributes values.This is called as map Object

Example: Setting an Attribute Value using Map Object

Give it a TRY! » Note:The attributes of src and style are used to style the img element


Setting an Attribute Value Dynamically using attr(name, function) method

The jQuery method of attr(name, function) uses a function to set attributes, the function can set attributes dynamically or apply attributes to multiple elements.

Example: Setting an Attribute Value Dynamically using attr(name, function) method

Give it a TRY! » Note: The method is used to set the height using attr() method with function as an argument.


Removing an Attribute using removeAttr(attribute) method

The jQuery method of removeAttr(attribute) is used to remove an attribute from each element in the set of matched elements.

If method removeAttr() uses the JavaScript function removeAttribute , but it has the advantage of being able to be chained to a jQuery Selector Expression.

Example: Removing an Attribute using removeAttr(attribute) method

Give it a TRY! » Note:The parameter of the method removeAttr() is an attribute


jQuery method prop() and removeProp()

The jQuery method of prop() is used to return the value of the specified property for the first element in the jQuery object.

There is a prop() method for every attr() method, the difference is that the prop() methods deal with properties defined by the HTMLElement, while the attr() method uses attributes.

Example: Read Set and Remove properties using the Method prop() and removeProp()

Give it a TRY! » Note:The parameter of the method removeAttr() is an attribute