Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Traversing in jquery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 39
    Comment on it

    While writing JQuery you need to understand the traversing of elements for applying CSS to it or perform any action.

    Following is an example that will add CSS to the third list item of the list.

    <html>
    
       <head>
          <title>The JQuery Example</title>
          <script type = "text/javascript" 
             src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    		
          <script type = "text/javascript" language = "javascript">
             $(document).ready(function() {
                $("li").eq(2).addClass("selected");
             });
          </script>
    		
          <style>
             .selected { color:red; }
          </style>
       </head>
    	
       <body>
    	
          <div>
             <ul>
                <li>list item 1</li>
                <li>list item 2</li>
                <li>list item 3</li>
                <li>list item 4</li>
                <li>list item 5</li>
                <li>list item 6</li>
             </ul>
          </div>
    		
       </body>
    	
    </html>

    You can also do filtering by using this.

    This will apply CSS to the middle content of the list item.

    <html>
    
       <head>
          <title>The JQuery Example</title>
          <script type = "text/javascript" 
             src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    		
          <script type = "text/javascript" language = "javascript">
             $(document).ready(function() {
                $("li").filter(".middle").addClass("selected");
             });
          </script>
    		
          <style>
             .selected { color:red; }
          </style>
       </head>
    	
       <body>
    	
          <div>
             <ul>
                <li class = "top">list item 1</li>
                <li class = "top">list item 2</li>
                <li class = "middle">list item 3</li>
                <li class = "middle">list item 4</li>
                <li class = "bottom">list item 5</li>
                <li class = "bottom">list item 6</li>
             </ul>
          </div>
    		
       </body>
    	
    </html>

    To find descendant elements in jquery following code is used

    <html>
    
       <head>
          <title>The JQuery Example</title>
          <script type = "text/javascript" 
             src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    		
          <script type = "text/javascript" language = "javascript">
             $(document).ready(function() {
                $("p").find("span").addClass("selected");
             });
          </script>
    		
          <style>
             .selected { color:red; }
          </style>
       </head>
    	
       <body>
          <p>This is 1st paragraph and <span>THIS IS RED</span></p>
          <p>This is 2nd paragraph and <span>THIS IS ALSO RED</span></p>
       </body>
    	
    </html>

     

    Javascript

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: