JQuery Selector Next Siblings

From CoderLearner
Jump to: navigation, search

Description

Select all the siblings element that follow the previous element. The siblings element and the previous must have the same parent.

Syntax

$("previous ~ sibling")

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>
      <style>
         .sp1 {
            width:50px;
            height:50px;
            background-color:yellow;
         }
         .sp2 {
            background-color: green;
            width:180px;
            height:80px;
         }
      </style>
      <script>
         $().ready(function() {
            $('div ~ span').addClass('sp1');
            $('span ~ p').addClass('sp2');
         });
      </script>
   </head>
   <body> 
      <div>
         <p>Photos</p>
         <span>Space</span>
         <ul>
            <li>Car</li>
            <li>Fly</li>
         </ul>
         <p>Mark it</p>
         <p>Job and Save</p>
      </div>
      <div>

      </div>
      <span>
         Drop by and visit you!
      </span>
   </body>
</html>

Try It