Jquery Set Attribute Value Example

11-Apr-2023

.

Admin

Hi Guys

In this blog, I will explain how to jquery set attribute value. we are showing a full example of set attribute value using jquery For setting attributes we pass two parameters, name and value in the attr() method. This will set all elements with the named attribute using the value passed by the method.

jQuery set attribute syntax: attr(name, value) – name could be any html attribute.

In below example the value of the ‘alt’ is set using the attr(name, value) method.

Example


<!DOCTYPE html>

<html>

<head>

<title>Jquery Set Attribute Value Example-nicesnippets.com</title>

</head>

<style type="text/css">

.design{

color:red;

}

</style>

<body>

<h1>Nicesnippets.com</h1>

<button>Set Attribute</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>

<script>

$(document).ready(function(){

$("h1").attr("class", "design");

$("button").attr("disabled", "disabled");

});

</script>

</body>

</html>

It will help you...

#Jquery