jQuery tag name selector

Syntax and Description

To select elements by tag name, use the tag name in the selector. For example, $("div") retrieves all of the divs in a document. The syntax of tag name selector is:

$('T')

$('T') selects all elements that have a tag name of T.

Examples

  • $('div') selects all elements with a tag name of div in the document.
  • $('em') selects all elements with a tag name of em in the document.

The following code shows how to find every DIV element.


<html>
<head>
  <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
  <script type="text/javascript">
      $(document).ready(function(){<!--from  w w w . j  a  v  a 2  s. c  o  m-->
          $("div").css("border","3px solid red");
      });
  </script>
</head>
<body>
  <body>
        <div class="myClass">div class="myClass"</div>
        <span class="myClass">span class="myClass"</span>
  </body>
</html>

Click to view the demo

Select all div tags

The following code use tag name to select all div tags.


<!DOCTYPE html> 
<html>
<head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"> 
    </script> 
    <script> 
        $(document).ready(function(){ <!--from   w w  w.j a v  a2s.c o  m-->
            var wrappedElements = $("div"); 
            document.writeln(wrappedElements.length); 
        });
    </script> 
</head> 
<body>
    <div id="1"></div> 
    <div id="2"></div> 
    <div id="3"></div>
</body> 
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities