jQuery Examples - $("ul li:first")



Description

"$("ul li:first")" get only the first <li> element of the <ul>.

Syntax

Here is the simple syntax to use this selector −

$("tagname child-element:first")

Parameters

Here is the description of all the parameters used by this selector −

  • tagname − Any standard container tag name like ol, ul etc.

  • child-element − list items li

  • :first − filter to get first element.

Returns

This selector returns the first element.

Example

<html>
   <head>
      <title>The Selector Example</title>
      <script type = "text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            /* Select link with an id of first and a class of big.*/
            $("ul li:first").css("background-color", "yellow");
         });
      </script>
   </head>
	
   <body>
      <ul>
         <li><a id = "first" class="big" href="#">Big</a></li>
         <li><a id = "first" class="medium" href="#">Medium</a></li>
         <li><a id = "first" class="small" href="#">Small</a></li>
      </ul>
   </body>
</html>

This will produce following result −

jqueryexamples_selectors.htm
Advertisements