<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("div").first().css("background-color", "blue"); }); </script> </head> <body> <h4>Example of first() </h4> <p>This is a paragraph. </p> <div style="border: 1px solid gray;"> This is a div one </div> <br> <div style="border: 1px solid gray;"> This is a div two </div> <br> <div style="border: 1px solid gray;"> This is a div three </div> </body> </html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("div").last().css("background-color", "blue"); }); </script> </head> <body> <h4>Example of last() </h4> <p>This is a paragraph. </p> <div style="border: 1px solid gray;"> This is a div one </div> <br> <div style="border: 1px solid gray;"> This is a div two </div> <br> <div style="border: 1px solid gray;"> This is a div three </div> </body> </html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("label").eq(2).css("background-color", "blue"); }); </script> </head> <body> <h4>Eample of eq() </h4> <label>Label on index 0. </label> <br> <label>Label on index 1. </label> <br> <label> Label on index 2. </label> <br> <label>Label on index 3. </label> </body> </html>
The filter() method gives you a chance to indicate a criteria. Elements that don't coordinate the criteria are expelled from the determination, and those that match will be returned.
This example selects all <label> elements with the class selectMe and changes the color of those labels
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("label").filter(".selectMe").css("background-color", "blue"); }); </script> </head> <body> <h4>Eample of filter() </h4> <label>I am Label 1. </label> <br> <label class="selectMe">I am Label 2. </label> <br> <label class="selectMe">I am Label 3. </label> <br> <label>I am Label 4. </label> </body> </html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("label").not(".doNotSelectMe").css("background-color", "blue"); }); </script> </head> <body> <h4>Eample of not() </h4> <label>I am Label 1. </label> <br> <label class="doNotSelectMe">I am Label 2. </label> <br> <label class="doNotSelectMe">I am Label 3. </label> <br> <label>I am Label 4. </label> </body> </html>