jQuery ID selector

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);
            });<!--from  w ww .  j av a2s.c  om-->
        </script> 
    <body> 
        <div id="main">
            <ul id="myList"> 
                <li>foo</li> 
                <li>bar</li>
            </ul> 
        </div> 
    </body> 
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities