How to add attributes to an HTML element in jQuery?

Given an HTML document containing some elements and the task is to add the attributes to the HTML elements.
Approach: Given an HTML document containing <label>, <input> and <button> elements. The <input> element contains the type and id attribute. When user click on the button, the jQuery function is called. We use $(selector).attr() method to add the attributes. Here, we are adding the placeholder attribute to the input field.
Syntax:
$("#add-attr").click(function () {
    $("#addAttr").attr("placeholder", "zambiatek");
});
Example:
HTML
| <!DOCTYPE HTML> <html>  Â<head>     <metahttp-equiv="Content-Type"content="text/html; charset=utf-8">       </script>  Â    <script>         $(document).ready(function () {             $("#add-attr").click(function () {                 $("#addAttr").attr("placeholder", "zambiatek");             });         });     </script> </head>  Â<bodystyle="text-align: center;">     <h1style="color: green;">         zambiatek     </h1>  Â    <h4>         How to add attributes to an HTML element in jQuery?     </h4>  Â    <labelfor="addAttr"></label>     <inputtype="text"id="addAttr">  Â      <buttonid="add-attr">Add Placeholder Attribute</button> </body>  Â</html> | 
Output:
 
				 
					



