{"id":12262,"date":"2022-08-03T13:13:53","date_gmt":"2022-08-03T17:13:53","guid":{"rendered":"http:\/\/local.brightwhiz\/?post_type=snippets&p=12262"},"modified":"2022-08-03T13:13:56","modified_gmt":"2022-08-03T17:13:56","slug":"use-jquery-to-add-an-attribute-to-an-html-element","status":"publish","type":"snippets","link":"http:\/\/local.brightwhiz\/snippets\/use-jquery-to-add-an-attribute-to-an-html-element\/","title":{"rendered":"How to Use jQuery to Add an Attribute to an HTML Element"},"content":{"rendered":"\n
This first example shows how to add a single attribute to an HTML element.<\/p>\n\n\n\n
$('#elementId').attr('attributeName', 'attributeValue');<\/code><\/pre>\n\n\n\nThis is how setting multiple attributes works. Quotes are optional when defining the attribute name.<\/p>\n\n\n\n
$('#elementId').attr({\n id: "TheId", \n title: "Meaningful details",\n class: "class-name", \n "data-name": "Alexis" \/\/ attributes names which contain dashes(-), should be wrapped in quotes.\n});<\/code><\/pre>\n","protected":false},"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[953,27,15,16],"tags":[303,333,424,433,471,638],"yoast_head":"\nHow to Use jQuery to Add an Attribute to an HTML Element<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n