JQuery Selector Next Adjacent Siblings

From CoderLearner
Jump to: navigation, search

Description

Select all next elements matching next that are immediately preceded by a sibling previous.

Syntax

$("previous + next")

Example 1

<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $().ready(function() {
            $('p + span').css('color','red');
            $('li + li').css('color','blue');
         });
      </script>
   </head>
   <body> 
      <div>
         <p>Photos</p>
         <span>Space</span>
         <ul>
            <li>Car</li>
            <li>Fly</li>
         </ul>
      </div>
   </body>
</html>

Try It

Example 2

Use $('element').next()

<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $().ready(function() {
            $('li').next().css('color','blue');
            
            $('p').next().css('color','red');
         });
      </script>
   </head>
   <body> 
      <div>
         <p>Photos</p>
         <span>Space</span>
         <ul>
            <li>Car</li>
            <li>Fly</li>
         </ul>
      </div>
   </body>
</html>