How to Add Attributes to an HTML Element in jQuery? Last Updated : 17 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To add attributes to an HTML element in jQuery, the attr() method can be used. This allows to set one or more attributes for a selected element, such as id, class, src, and href.Approach: Given an HTML document containing <label>, <input>, and <button> elements. The <input> element contains the type and id attribute. When the user clicks 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", "GeeksforGeeks");});Example: This example shows the use of the above-explained approach. HTML <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src= "https://code.jquery.com/jquery-3.5.1.min.js"> </script> </head> <body style="text-align: center;"> <h4> How to add attributes to an HTML element in jQuery? </h4> <label for="addAttr"></label> <input type="text" id="addAttr"> <button id="add-attr">Add Placeholder Attribute</button> <script> $(document).ready(function () { $("#add-attr").click(function () { $("#addAttr").attr("placeholder", "GeeksforGeeks"); }); }); </script> </body> </html> Output: Comment More infoAdvertise with us V vkash8574 Follow Improve Article Tags : JQuery HTML-Attributes jQuery-Questions Similar Reads HTML Attributes HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behavior, appearance, or functionality in a variety of ways. Each attribute has a name and a value 8 min read HTML align Attribute In HTML, the align attribute is used to control the alignment of elements on a webpage. Whether it's for text, images, or tables, the align attribute helps to position content in relation to its surrounding elements.Syntax<element_name align="left | right | center | justify">Attribute ValuesAt 4 min read HTML <img> align Attribute The HTML <img> align attribute sets an imageâs alignment relative to surrounding elements, allowing horizontal or vertical positioning. It is deprecated in HTML5 and replaced by CSS properties like float and vertical alignment for more flexible and modern alignment options.Note: The align attr 5 min read jQuery Tutorial jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc 8 min read HTML table border Attribute The HTML <table> border Attribute is used to specify the border of a table. It sets the border around the table cells. This attribute defines the visual presentation of the table by setting the thickness of the borders. A higher value results in a thicker border. Alternatively, setting the bor 6 min read HTML <td> valign Attribute The HTML <td> valign attribute specifies the vertical alignment of content within a table cell. It can accept values such as top, middle, bottom, or baseline to control the positioning of content. If not explicitly set, the content in a table cell is vertically aligned to the middle by default 2 min read HTML <td> align Attribute In HTML, the <td> (table data) element is used to define an individual cell within a table row (<tr>). It plays a crucial role in organizing and presenting structured data clearly. Whether displaying numerical values, text, or percentagesâsuch as 85% for progress or success ratesâthe 2 min read HTML <td> bgcolor Attribute The HTML <td> bgcolor attribute is used to specify the background color of a table cell. It basically, Specify the background color with a hexadecimal code for precise coloring and offers limited color choices compared to modern CSS styling. Note: It is not supported by HTML5. Syntax:<td b 2 min read HTML Attributes Complete Reference HTML attributes are special words placed inside the opening tag of an HTML element to define its characteristics. Each attribute has two parts:Attribute nameAttribute value (separated by an equal sign = and enclosed in double quotes " ").Syntax:<tag_name attribute_name="value"> Contents... 8 min read HTML font color Attribute The HTML font color attribute was historically used to specify text color within the <font> tag; although deprecated in HTML5 (used by less than 5% of modern websites), it's still useful to understand its legacy usage, while modern HTML relies on CSS for styling.Syntax<font color="color_nam 3 min read Like