Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • jQuery Filtering - first( ) method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    This method is used to return the first element of the selected elements.

    Example :
    HTML Code :

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <div style="border: 1px solid black;">
      <p>This is the first paragraph in a div.</p>
      <p>This is the last paragraph in a div.</p>
    </div><br>
    
    <div style="border: 1px solid black;">
      <p>This is the first paragraph in another div.</p>
      <p>This is the last paragraph in another div.</p>
    </div>
    
    </body>
    </html>
    

    jQuery Code :

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("div p").first().css("background-color", "yellow");
    });
    </script>
    

    Output :
    This is the first paragraph in a div.// first method change the color of this p tag i.e first ( ) method
    This is the last paragraph in a div.

    This is the first paragraph in another div.
    This is the last paragraph in another div.

 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: