Skip to content
Related Articles

Related Articles

How to use JavaScript variables in jQuery selectors ?

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 03 Aug, 2021

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 are 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 then execute the rest code.




<!DOCTYPE html>
  
<html>
  
<head>
    <title>JavaScript variables in jQuery slectors</title>
  
            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 fo 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 then execute the rest code.




<!DOCTYPE html>
<html>
  
<head>
    <title>JavaScript variables in jQuery slectors</title>
  
            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 with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!