How to select the multiple elements using tag name, id or class name in jQuery ?

Gets multiple elements using tag, id or class name in jQuery

$('p').css('color', 'blue');

Selects all the paragraph elements.

$('p, #id').css('color', 'blue');

Selects all the paragraph elements and other elements which having id is 'id'.

$('p, #id, .class-name').css('color', 'blue');

Selects all the paragraph elements and other elements which having id 'id' or class name 'class-name'.

Example jQuery code to get multiple elements using tag, id and class name

<html>
   <head>
      <title>The jQuery Example - To exclude </title>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>		
      <script type = "text/javascript">
      $(document).ready(function(event) {
          $('p, #id, .class-name').css('color', 'blue');
        });
      </script>		
   </head>	
   <body>
<div id='id'>line 0</div>
<div>line 1</div>
<div>line 2</div>
<p>line 3</p>
<div class="class-name">line 4</div>
<p>line 5</p>
   </body>
</html>
Output:
multiple elements selection output

Used tag name, id and class name in the selectors.

Getting all the paragraph elements and other elements which having id 'id' or class name 'class-name'.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^