How to add attributes to an HTML element in jQuery

  2474 views   2 years ago jQuery

Use the jQuery attr() method

You can use the jQuery attr() method to add attributes to an HTML element.

In the following example when you click on the "Select Checkbox" button it will add the checked attribute to the checkbox dynamically using jQuery.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Adding Attribute to HTML Element</title>
<script src=""></script>
            $('input[type="checkbox"]').attr("checked", "checked");
    <button type="button" class="add-attr">Select Checkbox</button>
    <input type="checkbox"> 


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]