Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Traverse in jQuery using filtering methods

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 173
    Comment on it

    Traversing in jQuery is basically used to search or select HTML elements or attributes of a web page. During the traversal, we are using filtering methods to manipulate the DOM. Some filtering methods describe as below...

     

    first(): This method searches the first element of selected HTML tag.

     

    Example:

    $("div span").first().text("Select very first <span> of a <div> from a web page and change their text").css("background-color", "yellow");

     

    In this example, we  selected very first <span> of a <div> from a web page and changed their text & background color .

     

    last(): This method searches a last element of selected HTML tag in a web page

     

    Example:

    $("div span").last().text("Select most last <span> of a <div> from a web page and change their text").css("background-color", "orange");

     

    In this example, we selected most last <span> of a <div> from a web page and changed their text & background color .

     

    Eq(): This method gives an element with a specified index of the selected HTML tag.

     

    Example:

     $("span").eq(3).css("font-weight", "bold");

     

    In this example, we selected fourth <span>(i.e. <span> with third index) from a web page and changed their font-wait.

     

    filter(): This method provides facility to specify the criteria with the selector and gives result according to matched conditions.

     

    Example:

    $("p,span").filter(".test").css("background-color", "red");

     

    In this example, we selected all <p> and <span> which have class "test"  from a web page and changed their background-color.

     

    not(): This method work unlike filter() and gives a result which does not match conditions

     

    Example:

    $("p,span").not(".test").css("background-color", "gray");

     

    In this example, we selected all <p> and <span> which haven't class "test"  from a web page and changed their background-color.

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    5. <script>
    6. $(document).ready(function(){
    7. //Using first()
    8. $("div span").first().text("Select very first <span> of a <div> from a web page and change their text").css("background-color", "yellow");
    9.  
    10. //Using last()
    11. //$("div span").last().text("Select most last <span> of a <div> from a web page and change their text").css("background-color", "orange");
    12.  
    13. //Using eq()
    14. //$("span").eq(3).css("font-weight", "bold");
    15.  
    16. //Using filter()
    17. //$("p,span").filter(".test").css("background-color", "red");
    18.  
    19. //Using not()
    20. //$("p,span").not(".test").css("background-color", "gray");
    21.  
    22. });
    23. </script>
    24. </head>
    25. <body>
    26.  
    27. <h3>Traverse using first() methods</h3>
    28.  
    29. <p>First paragraph in this page.</p>
    30.  
    31. <div style="border: 2px solid green;">
    32. <span>This is the First span.</span>
    33. <br>
    34. <span class="test">This is the Second span.</span>
    35. </div><br>
    36.  
    37. <div style="border: 2px solid gray;">
    38. <span class="test">This is the First span in Second div.</span>
    39. </br>
    40. <span>This is the Second span in Second div.</span>
    41. </div>
    42.  
    43. <p class="test">Last paragraph in this page.</p>
    44.  
    45. </body>
    46. </html>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: