How to use JavaScript variables in jQuery selectors ? Last Updated : 27 Apr, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to use JavaScript variables in jQuery selectors. In the following examples, it can be seen that we have used the values stored in JavaScript Variables used inside the jQuery Selectors. Example 1: The concatenation technique can be applied in order to use the values stored in JavaScript variables. In the following example, whenever the button is clicked, the content present inside the <span> element is appended to the <p> element. Then we will use the ready() method that helps to load the whole page and then execute the rest code. html <!DOCTYPE html> <html> <head> <title>JavaScript variables in jQuery slectors</title> <script src="https://code.jquery.com/jquery-3.4.1.js" integrity= "sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"> </script> </head> <body> <div> <button id="button" style="color: green;"> Click Me </button> <p id="firstpara"> <span id="span" style="color: green;"> A Computer Science Portal for Geeks<br> </span> </p> </div> <script> $(document).ready(function() { $("#button").click(function() { var paraId = "firstpara"; var spanId = "span"; $("#" + paraId).append($("#" + spanId).html()); }); }) </script> </body> </html> Output: Example 2: The following example changes the color of the text when the link is pressed. In this example, javascript:void(0); is used inside <a> element. Then we will use the ready() method helps to load the whole page and then execute the rest code. html <!DOCTYPE html> <html> <head> <title>JavaScript variables in jQuery slectors</title> <script src="https://code.jquery.com/jquery-3.4.1.js" integrity= "sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"> </script> </head> <body> <form> <p> Text: <input type="text"> </p> <br> <a href="javascript:void(0);"> Change the color of Text </a> </form> <script> $(document).ready(function() { $("a").click(function() { var type = $("input").attr("type"); var attribute = "color"; var color = "green"; $("input[type=" + type + "]") .css(attribute, color); }); }) </script> </body> </html> Output: jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples. Comment More infoAdvertise with us Next Article How to use JavaScript variables in jQuery selectors ? A AnasShamoon Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies JQuery Technical Scripter 2019 jQuery-Misc JavaScript-Misc +3 More Similar Reads How to convert jQuery to JavaScript ? JavaScript is an object orient programming language designed to make web development easier and more attractive. In most cases, JavaScript is used to create responsive, interactive elements for web pages, enhancing the user experience. jQuery is an open-source JavaScript library that simplifies the 2 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 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 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 each element with a specific attribute, with a specific string value (like "geeks") or starting string followed by a hyphen (like "geeks-forgeeks"). Syntax: $("[attribute|='value']")Parameter: attribute : This parameter is required to specify 1 min read jQuery [attribute~=value] Selector jQuery [attribute~=value] Selector Select all elements with a name attribute that contains the specific string. Syntax: $("[attribute~='string']")Parameter: attribute: Specifies which attribute is used to find.string: Specifies the string value to find.Example 1: In this example, we will select all 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 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 How to use *= operator in jQuery attribute selector ? This article will show you an idea of using the *= operator in jQuery attribute selectors. The *= operator is used to select the sub-string attribute and apply operations on it. Syntax: $("div[myAttr*='GFG']").jQueryMethod({ // Code }) Approach: We will create HTML div elements with some attributes. 2 min read How to select DOM Elements in JavaScript ? Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo 3 min read Like