jQuery | [attribute] Selector

The [attribute] Selector is an inbuilt selector in jQuery, used to select all the elements with the specified attribute.

Syntax:

$("[attribute_name]")

Parameter:



  • attribute_name: It is the required parameter which specify the attribute to be select.

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    
    <script>
        $(document).ready(function() {
            $("[class]").css("color",
                             "green");
        });
    </script>
</head>
  
<body>
    <center>
  
        <!-- Class attribute-->
  
        <h3 class>GeeksforGeeks</h3>
        <p>A computer science portal for geeks</p>
  
        <!--id attribute-->
  
        <p id>Geek1</p>
  
        <!--class attribute-->
  
        <p class>Geek2</p>
        <p>Geek3</p>
  
        <!--class attribute-->
  
        <div class> Thank You! </div>
  
    </center>
</body>
  
</html>

chevron_right


Output:

In the above example, all the elements with the specified attribute (class) are formatted into green color i.e, “GeeksforGeeks”, “Geek2” and “Thank You!”.

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    
    <script>
        $(document).ready(function() {
            $("[class]").css("color",
                             "green");
        });
    </script>
</head>
  
<body>
    <center>
  
        <!-- Class(demo) attribute-->
  
        <h3 class="demo">GeeksforGeeks</h3>
        <p>A computer science portal for geeks</p>
  
        <!--id attribute-->
  
        <p id>Geek1</p>
        <p>Geek2</p>
  
        <!--class(test) attribute-->
  
        <p class="test">Geek3</p>
  
        <!--class(sample) attribute-->
  
        <div class="sample"> Thank You! </div>
  
    </center>
</body>
  
</html>

chevron_right


Output:

In the above example, all the elements with the specified attribute (class) are formatted into green color rather considering the value of the attribute. “GeeksforGeeks”, “Geek3” and “Thank You!” are formatted.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.