When we are manipulating our pages, we sometimes need to get the value stored as an attribute to an HTML tag, or set an attribute to some value. This is accomplished via the .attr() method. This parameter takes two parameters - the attribute to work with, and the value to set the attribute to. Not specifying the second parameter will retrieve the current value.
//getting the ID
alert( $(".target").attr("id") );
//
//Setting the ID
$(".target").attr("id", "targetID");
//
//Changing where a form will be submitted to
$("#myform").attr("action", "otherpage.php");
The .attr() method can be used on any element. If the asked for attribute does not exist, null or undefined will be returned when requesting it's value. If setting a non-existent attribute, that attribute will be created.
If more than one attribute needs to be set then an object containing key/value properties can be used.
$("#myimg").attr({
src : "myimage.gif",
title : "My Image",
alt : "My Image",
border : 0
});