Universal (*)

Description and Syntax

$('*')

selects all elements.

Examples

  • $('*') all elements in the document
  • $('p > *') all elements that are children of a paragraph element

<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/style/jquery-1.8.0.min.js"> 
        </script>
        <script>
<!--   w  w w  .  j  a  v  a2s.c  o  m-->
            $(document).ready(function(){ 
                var allElements = $("*"); 
                document.writeln(allElements.length);
            }); 
        </script> 
    <body> 
        <div id="main">
            <ul id="myList"> 
                <li>A</li> 
                <li>B</li>
            </ul> 
        </div> 
    </body> 
</html>

Click to view the demo

The code above generates the following result.

Universal (*)

Select all elements under a certain element

The following code selects all elements under a certain id.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--  ww  w  . j  av a  2  s  .  c  om-->
                
           $("#main > *").css("border", "3px solid red");
                
        });
    </script>
  </head>
  <body>
    <body>
      <span id="main">
        <div>asdf</div>
        <button>Child</button>
        <span>A Span</span>
      </span>
    </body>
</html>

Click to view the demo

The code above generates the following result.

Universal (*)

Match all elements

The following code selects every element (including head, body, etc).


<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  ava 2  s  .com-->

            $("*").css("border","3px solid red");

        });
    </script>
  </head>
  <body>
    <body>
          <div>A div</div>
          <button>0</button>
          <button>1</button>
          <button>2</button>
          <button>3</button>
          <span></span>
    </body>
</html>

Click to view the demo

The code above generates the following result.

Universal (*)