ID

Syntax for ID selector

$('#myid') selects the unique element with an ID equal to myid.

Examples

  • $('#myid') selects the unique element with id="myid".
  • $('p#myid') selects a single paragraph with an ID of myid.
  • $("div#elementId") selects all divs with an ID of elementId.

<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/style/jquery-1.8.0.min.js">
        </script>
        <script>
            $(document).ready(function(){ 
                var wrappedElements = $("ul#myList"); 
                document.writeln(wrappedElements.length);
            });<!--  w  w w . j  ava  2  s  . c  o  m-->
        </script> 
    <body> 
        <div id="main">
            <ul id="myList"> 
                <li>foo</li> 
                <li>bar</li>
            </ul> 
        </div> 
    </body> 
</html>

Click to view the demo

The code above generates the following result.

ID