Hello friends,
Today we learn to change the multiple attribute value using jQuery. To set multiple attribute value we will use a jQuery attr() method. This method is used for changing the attribute values. We can change multiple attributes with the help of attr() method at the same time.
Let's understand this with an example. Suppose we have a link and a button. We need to change the href and title of link on click of the button. To perform this task we need to execute the following code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#changelink").attr({
"href" : "http://newsite.com",
"title" : "This is the new title"
});
});
});
</script>
</head>
<body>
<a href="http://oldsite.com" title="old title" id="changelink">Link</a>
<button>Click here to change the attribute</button>
</body>
</html>
0 Comment(s)