Easy Tutorial
❮ Jquery Callback Jquery Plugin Treeview ❯

jQuery attr() Method

jQuery HTML/CSS Methods

Example

Set the width attribute of an image:

$("button").click(function(){
    $("img").attr("width","500");
});

Definition and Usage

The attr() method sets or returns attributes and values of the selected elements.

When this method is used to return attribute values, it returns the value of the first matched element.

When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.


Syntax

Return an attribute's value:

Set attribute and value:

Set attribute and value using a function:

Set multiple attributes and values:

Parameter Description
attribute Specifies the name of the attribute.
value Specifies the value of the attribute.
function( index,currentvalue) Specifies a function that returns the attribute value to the set. <br> index - Receives the index position of the element in the set. <br> currentvalue - Receives the current attribute value of the selected element.

More Examples

Return Attribute Value

Set Attribute and Value Using a Function

Set Multiple Attribute and Value Pairs


jQuery HTML/CSS Methods

❮ Jquery Callback Jquery Plugin Treeview ❯