How to Set Attribute Value in Jquery Example ?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Today now in this blog, I will explain you how to set attribute value by using jquery. Here i will showing a full example of set attribute value by using jquery. For setting attributes we need to pass two parameters, one is name and another one is value in the attr() method. So this will set all elements with the named attribute by using the value passed by the method.

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

Now here in the below example the value of the ‘alt’ is set by 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>

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →