How to set attribute without value using jQuery ? Last Updated : 08 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report There are many ways to set attributes without value using jQuery. jQuery prop() method is used to get or set the values of attributes. To change the properties like checked, default checked, disabled, selectedIndex, default selected and selected state of Document Object Model, use the jQuery .prop() method. Also, we can use the attr() method to do that. Below both, the methods are described with the help of examples.Method 1: You can use the jQuery attr() method which a function for setting attributes for set of matching elements and then pass an empty string. It will be equivalent to setting attribute without value.Syntax : .attr(attribute-name, value to be passed)Example 1 : html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Setting attribute without value </title> <style> div { color: blue; } </style> <script src="https://code.jquery.com/jquery-3.4.1.js"> </script> </head> <body> <p> Let us learn about <b title="jQuery, attr() ">method</b> for setting attributes. </p> To set the value of attribute, use : <div></div> <script> let title = $("b").attr("title"); $("div").text(title); </script> </body> </html> Output: Method 2: This is done by using jQuery prop() method which is used to develop name only attributes. Syntax: prop(para1, para2)Example 1 : html <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script> <script> $(document).ready(function () { $('#btnDiv').click(function () { var content = $('.active').attr('class'); $("#showDivContent").text(content); }); $('#btnPara').click(function () { var style = $('p').prop('style'); var styleName = style.fontWeight $("#showParastyle").text(styleName); }); }); </script> <style> body { width: 450px; height: 300px; margin: 10px; float: left; } .active { background-color: yellow; margin: 5px 0 0 0; } h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <b> Setting attributes without value using jQuery </b> <br> <br> <button id="btnDiv"> Get div class name </button> <button id="btnPara"> Get paragraph style </button> <div id="showDivContent" class="active"> This is div. </div> <p id="showParastyle" style="font-size:16px; font-weight:bold;"> This is paragraph. </p> <div class="active"> This is div. </div> </body> </html> Output : Example 2: html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Setting attributes without values using jQuery </title> <style> p { margin: 20px 0 0; } b { color: blue; } </style> <script src= "https://code.jquery.com/jquery-3.4.1.js"> </script> </head> <body> <h1 style="color:green"> GeeksforGeeks </h1> <label for="checkboxId"> Check me </label> <input id="checkboxId" type="checkbox" checked="checked"> <p></p> <script> $("input").change(function () { let $input = $(this); $("p").html(".attr( 'checked' ): <b>" + $input.attr("checked") + "</b><br>" + ".prop( 'checked' ): <b>" + $input.prop("checked") + "</b><br>" + ".is( ':checked' ): <b>" + $input.is(":checked") + "</b>"); }).change(); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article jQuery [attribute*=value] Selector G geetanjali16 Follow Improve Article Tags : Web Technologies JQuery jQuery-Misc Similar Reads jQuery [attribute!=value] Selector The jQuery [attribute!=value] selector in jquery is used to select each element that doesn't have the specified attribute and value. Syntax: $("[attribute!='value']")Parameter: attribute: This parameter is required to specify the attribute to be searched.value: This parameter is required to specify 1 min read jQuery | [attribute=value] Selector The jQuery [attribute=value] selector is used to select and modify HTML elements with specified attribute and value. Parameter Values: attribute: Used to specify attribute to find. Value: Used to specify value to find. Syntax: $("[attribute=value]") Example-1: This example selects the elements havin 1 min read jQuery [attribute*=value] Selector jQuery ("[attribute*=value]") Selector is used to select all elements with attribute specified by attribute parameter that contains the word specified by the value parameter. Syntax: $("[attribute*='value']")Parameters: This selector has two parameters. attribute: attribute specifies the attributes 2 min read jQuery [attribute$=value] Selector The jQuery [attribute$=value] selector is used to select each element with a specific attribute, with a specific ending string. Syntax: $("[attribute$='value']")Parameter: attribute: This parameter is required to specify the attribute to find.value: This parameter is required to specify the string i 1 min read jQuery [attribute^=value] Selector The jQuery [attribute^=value] selector is used to select all elements with a given attribute specified by an attribute parameter that starts with the word specified by value parameter. Syntax: $("[attribute^='value']")Parameters: This selector contains two parameters which are listed below: attribut 2 min read How To Set Custom Attribute Using JavaScript? In modern web development manipulating HTML elements dynamically is a common requirement. One such task is setting custom attributes on elements to store additional data or metadata. Custom attributes referred to as data attributes allow developers to attach extra information to elements. In this ar 2 min read Like