Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
This Jquery attr() method is specifically used to set attributes and values of selected elements. WE can also return the value of the attributes based on the attribute name.
If we also wish to set the attribute value, the Jquery attr() allows us to set one or more attribute values for a set of matched elements.
When it is used to return the attribute value; it returns the value of first matched element.
Syntax
Let us also have a look at the syntax of Jquery attr() :
a. To set the attribute and value:
$(selector).attr(attributeName, attributeValue)
Explanation: We also have to pass the name of the attribute and the corresponding value that we wish to assign.
b. To set the attribute and value using the function:
$(selector).attr(attributeName,function(indexPosition, currentAttributeValue))
Explanation: We also have to pass the name of the attribute and define the task we need to perform on that particular attribute within the function.
c. To set multiple attributes and values:
$(selector).attr({ attributeName: attributeValue, attributeName: attributeValue, attributeName: attributeValue,………… })
Explanation: also Based on the selector, bypassing multiple attributes and its value in the form of key-value pair we can set multiple attributes and its value.
d. Returning the value of an attribute:
$(selector).attr(attributeName)
Explanation: To also return the value of an attribute, we simply will have to pass the name of the attribute.
As seen in the above syntax; the attr() method can be also used to set the attribute and value.
It also allows us to set multiple values and attributes. We can return the value of an attribute.
To use jquery, we can download jquery from jquery.com.
The attr() method also comes with a callback function. A callback function then performs the action that is been defined in that function. Let us see through the following examples of how can we perform all these operations.
In the below examples we have also used “id” as the selector. Based on the “id” we will be setting and returning the attributes and its value.
Here are the following examples mention below
Example #1
Example of setting the attribute and also value
Description: In the below code, we will be setting the attribute value also of the name from “abc” to “pqr” on the button click action.
Code snippet:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).readyalso(function(){ alert($(“#x”).attr(“name”))
$(“button”).click also (function(){
$(“#x”).attr(“name”, “pqr”);
alert($(“#x”).attr(“name”))
});
});
</script>
</head>
<body>
<p><a name=”abc” id=”x”></a></p>
<button>Also Change name value</button>
<p>Also Click on the button to change the value from “abc” to “pqr”</p>
</body>
</html>
Example #2
Example of setting the attribute and also value using a callback function. In the below code snippet; the initial value of roll no is 11 which you will be able to see as soon as you run the program. Using a function we also have set the value to 10 of the attribute rollNo.
Code snippet:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$ also (document).ready(function(){ alert($(“#n”).attr(“rollNo”))
$(“button”).click(function(){
$(“#n”).attr(“rollNo”, function(index,currentvalueofrollno){ return currentvalueofrollno – 1 ;
});
alert($(“#n”).attr(“rollNo”))
});
});
</script>
</head>
<body>
<p><a rollNo= 11 id=”n”></a></p>
<button>Change roll no value as well as name</button>
<p>Click on the button to also change the roll no value from 11 to 10</p>
</body>
</html>
Example of setting multiple attributes and values
Description:
In this example, the initial value also will be effected rollNo is 16 and the name is “xyz” which will be changed to 17 and “abc”. We will be setting multiple attributes at the same time. Refer the below code snippet for the same:
Code snippet:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){ alert($(“#n”).attr(“rollNo”))
alert($(“#n”).attr(“name”))
$(“button”).click(function(){
$(“#n”).attr({“rollNo”:”17″ , “name” : “abc” }); alert($(“#n”).attr(“rollNo”))
alert($(“#n”).attr(“name”))
});
});
</script>
</head>
<body>
<p><a id=”n” rollNo=”16″ name=”xyz”></a></p>
<button>Change roll no and also name value</button>
<p>Click on the button to change the and name and roll no value</p>
</body>
</html>
Example of returning the value also of an attribute. In the below scenario of returning the value of an attribute; the value of the name is “abc”. We will be also returning the same on the button click action.
Code snippet:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“button”).click(function(){
alert($(“#x”).attr(“name”))
});
});
</script>
</head>
<body>
<p><a name=”abc” id=”x”></a></p>
<button>View name and also value</button>
<p>Click on the button to also view the name value</p>
</body>
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“.btn1”).click(function(){
$(“p”).fadeOut();
});
$(“.btn2”).click(function(){
$(“p”).fadeIn();
});
});
</script>
</head>
<body style=”background-color: antiquewhite;”>
<h3>Using the method “fadeIn()” without any parameter:</h3>
<p style=”font-family: Arial, Helvetica, sans-serif;display:none;”>This paragraph is appeared after the “Fade In” button is also clicked </p>
<button class=”btn1″> also Fade out</button>
<button class=”btn2″> also Fade in</button></div>
</body>
</html>
/html>
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“.btn1”).click(function(){
$(“p”).fadeOut(5000);
});
$(“.btn2”).click(function(){
$(“p”).fadeIn(5000);
});
});
</script>
</head>
<body style=”background-color: antiquewhite;”>
<h3>Using the method “fadeIn()” with ‘duration’parameter:</h3>
<p style=”font-family: Arial, Helvetica, sans-serif;display:none;”>This paragraph is also appeared in 5000 millisecond,after the “Fade In” button is clicked </p>
<button class=”btn1″>Fade out</button>
<button class=”btn2″>Fade in</button></div>
</body>
</html>
Code:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“.btn1”).click(function(){
$(“p”).fadeOut(3000,”linear”);
});
$(“.btn2”).click(function(){
$(“p”).fadeIn(3000,”linear”);
</script>
</head>
<body style=”background-color: antiquewhite;”>
<h3> also Using the method “fadeIn()” with ‘easing’parameter:</h3>
<p style=”font-family: Arial, Helvetica, sans-serif;display:none;”>This paragraph is also appeared in a linear speed,after the “Fade In” button is clicked </p>
<button class=”btn1″>Fade out</button>
<button class=”btn2″>Fade in</button></div>
</body>
</html>
Code:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“.btn1”).click(function(){
$(“p”).fadeOut(function(){
alert(“fadeOut() method is finished!”););
$(“.btn2”).click(function(){
$(“p”).fadeIn(function(){
alert(“fadeIn() method is finished!”);
</script>
</head>
<body style=”background-color: antiquewhite;”>
<h3>Using the method “fadeIn()” with ‘callback’parameter:</h3>
<p style=”font-family: Arial, Helvetica, sans-serif;display:none;”>The Alert message also appeared from callback function<br>
after the text message also being visible. </p>
<button class=”btn1″>Also Fade out</button>
<button class=”btn2″>Also Fade in</button></div>
</body>
</htmCode:
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“.btn1”).click(function(){
$(“.mydiv”).fadeOut()
});
$(“.btn2”).click(function(){
$(“.mydiv”).fadeIn()
});
});
</script>
</head>
<body style=”background-color: antiquewhite;”>
<div>
<h4>Click on ‘Also Fade In’ button</h4>
<div class=”mydiv” style=’display:none;font-family: Arial, Helvetica, sans-serif;’>The fading effect fadeIn() is used on ‘mydiv’ class</div>
<button class=”btn1″>Also Fade out</button>
<button class=”btn2″>Also Fade in</button></div>
</body>
</html>
Thus we could learn how the jquery attr() method can not only be used to set attributes and value but can also set multiple attributes and values. We could also see how to return the value of an attribute and the use of callback function within the jquery attr() method.
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.