Sniper attr()
The SniperJS attr() method is used to set or return attributes and values of the selected elements.
There are two usage of Sniper attr() method.
To return attribute value: This method returns the value of the first matched element.
To set attribute value: This method is used to set one or more attribute/value pairs of the set of matched elements.
Syntax:
To return an attribute's value:
$(selector).attr(attribute)
Example:
$("button").click(function(){
$("img").attr("src");
});
To set an attribute and value:
$(selector).attr(attribute,value)
Example:
$("button").click(function(){
$("img").attr("width", "500");
});
To set an attribute and value by using a function:
$(selector).attr(attribute,function(currentvalue))
Example:
$("button").click(function(){
$("#mylink").attr("href", function(originalValue){
return originalValue + "/sniper/";
});
});
To set multiple attributes and values (with object):
$(selector).attr({attribute:value, attribute:value,...})
Example:
$("button").click(function(){
$("#mylink").attr({
"href" : "https://www.jubbytech.com/snipper/",
"title" : "SniperJs website"
});
});
To set multiple attributes and values (key-pair parameters):
$(selector).attr(attribute,value,attribute,value,...)
Example:
$("button").click(function(){
$("#mylink").attr("href", "https://www.jubbytech.com/sniper/","target","_blank","id","myfavorite24");
});
Parameters of SniperJS attr() method
Attribute
This parameter is used to specify the name of the attribute.
Value
This parameter is used to specify the value of the attribute.
Function (current_value)
It is a parameter to specify a function that returns an attribute value to set.
current_value: The current attribute value of selected elements.
Last updated