1.Manipulating element properties
Command syntax:each(iterator)
Traverses all elements in the matched set invoking the passed iterator function for each.
Parameters
iterator (Function) A function called for each element in the matched set. The parameter passed to this function is set to the zero-based index of the element within the set, and the element itself is available as the this property of the function.
Returns
The wrapped set.
eg: $('img').each(function(n){
this.alt='This is image['+n+'] with an id of '+this.id;
});//This statement will invoke the inline function for each image element on the page, modifying its alt property using the order of the element and its id value.
2.Fetching attribute values
Command syntax:attr(name)
Obtains the values assigned to the specified attribute for the first element in the matched set.
The attr() command can be used to either fetch the value of an attribute from the first element in the matched set or set attribute values onto all matched elements.
Parameters
name (String) The name of the attribute whose value is to be fetched.
Returns
The value of the attribute for the first matched element. The value undefined is returned if
the matched set is empty or the attribute doesn’t exist on the first element.
3.Setting attribute values
Command syntax:attr(name,value)
Sets the named attribute onto all elements in the wrapped set using the passed value.
Parameters
name (String) The name of the attribute to be set.
value (String|Object|Function) Specifies the value of the attribute. This can be any JavaScript expression that results in a value, or it can be a function. See the following
discussion for how this parameter is handled.
Returns
The wrapped set.
eg: $('*').attr('title',function(index) {
return 'I am element ' + index + ' and my name is ' +
(this.id ? this.id : 'unset');
});
$("a[href^=http://]").attr("target","_blank");// set all the links open in a new window!
Command syntax:attr(attributes)
Sets the attributes and values specified by the passed object onto all elements of the matched set
Parameters
attributes (Object) An object whose properties are copied as attributes to all elements in the wrapped set
Returns
The wrapped set
eg: $('input').attr(
{ value: '', title: 'Please enter a value' }
);
note: Internet Explorer won’t allow the name attribute of <input> elements to
be changed. If you want to change the name of <input> elements in
Internet Explorer, you must replace the element with a new element pos-
sessing the desired name.
4.Removing attributes
Command syntax:removeAttr(name)
Removes the specified attribute from every matched element
Parameters
name (String) The name of the attribute to be removed
Returns
The wrapped set