jQuery next( [selector] ) Method

« Previous Chapter Next Chapter »

Description:

The next( [selector] ) method gets a set of elements containing the unique next siblings of each of the given set of elements.

Syntax:

Here is the simple syntax to use this method:

Example

selector.next( [selector] )
                                        

Parameters:

Here is the description of all the parameters used by this method:

  • selector: The optional selector can be written using CSS 1-3 selector syntax. If we supply a selector expression, the element is unequivocally included as part of the object. If we do not supply one, the element would be tested for a match before it was included.

Example:

Following is a simple example a simple showing the usage of this method. Try this example without passing selector in next() method:

Example

<html>
<head>
   <script type="text/javascript"  src="../../js/jquery-2.2.0.js"></script>
   <script type="text/javascript" language="javascript">
     $(document).ready(function(){
       $("p").next(".selected").addClass("hilight");
     });
  </script>
  <style>
      .hilight { background:yellow; }
  </style>
</head>
<body>
  <p>NewFile.</p>
  <p class="selected">NewFile. Again</p>
  <div><span>And Again</span></div>
</body>
</html>
                                        

This would generate following result.

Example

<html>
<head>
   <script type="text/javascript"  src="../../js/jquery-2.2.0.js"></script>
   <script type="text/javascript" language="javascript">
     $(document).ready(function(){
       $("p").next(".selected").addClass("hilight");
     });
  </script>
  <style>
      .hilight { background:yellow; }
  </style>
</head>
<body>
  <p>NewFile.</p>
  <p class="hilight">NewFile. Again</p>
  <div><span>And Again</span></div>
</body>
</html>
                                        
Code It Online

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!