Layout Options

  • Fixed Header
    Makes the header top fixed, always visible!
  • Fixed Sidebar
    Makes the sidebar left fixed, always visible!
Introduction
Webconcept

Description

The .attr() method sets or returns attribute values of selected elements.

Note: As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.


Return Attribute Value

Return the value of an attribute for the selected element.


v1.0

This method has the form:

.attr(attribute)
Parameter Description
attribute Specifies the attribute to get the value of

Return Value

This form returns a String object.


Set Attribute/Value

Set the attribute and value of the selected elements.

v1.0

This method has the alternate form:

.attr(attribute,value)
Parameter Description
attribute Specifies the name of the attribute
value Specifies the value of the attribute

Return Value

This form returns a jQuery object.


Set Attribute/Value Using a Function

Using a function to set the attribute value for the selected elements.

v1.1

This method has the alternate form:

.attr(attribute,function(index,oldvalue))
Parameter Description
attribute Specifies the name of the attribute
function(index, oldvalue) Specifies a function that returns the attribute value to set.
  • index - (optional) Receives the index position of the selector
  • oldvalue - (optional) Receives the current attribute value of the selector

Return Value

This form returns a jQuery object.


Set Multiple Attribute/Value Pairs

Set one or more attributes and values for the selected elements.

v1.0

This method has the alternate form:

.attr({ attribute:value,attribute:value, ...})
Parameter Description
{attribute:value, attribute:value, ...} Specifies one or more attribute/value pairs

Return Value

This form returns a jQuery object.

Examples

Set the width attribute of an image:

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

Description

The .removeAttr() method removes a specified attribute from the selected elements.


v1.0

This method has the form:

.removeAttr(attribute)
Parameter Description
attribute (required) Specifies the attribute to remove

Return Value

This form returns a jQuery object.

Examples

Remove the style attribute from all p elements:

$("button").click(function(){
  $("p").removeAttr("style");
});