In this post, we will learn how to add attribute to an element in jQuery. So how we can do this, we will learn in this post.


How To Add Attribute To An Element In jQuery ?


Add Attribute To An Element 

To add attribute to an element, we use   attr()  jQuery method. In this given example when we click on the button then the  placeholder  attribute added. So how this works we will see in the given example.


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How To Add Attribute To An Element In jQuery ?</title>
</head>
<body>

<input type="text">
<br/><br/>
<button type="button">Add Attribute</button>

<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>


<script>

$(document).ready(function(){
$("button").click(function(){
$('input[type="text"]').attr("placeholder", "Type your text...");
});
});

</script>
</body>
</html> code-box



Summary

In this post we learned how to add attribute to an element in jQuery. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts. 

Post a Comment

Previous Post Next Post