over 9 years ago
This method is used to return the last element of the selected elements.This filter help users to filtering 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>
<!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").last().css("background-color", "yellow");
- });
- </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("div p").last().css("background-color", "yellow"); }); </script>
Output :
This is the first paragraph in a div.
This is the last paragraph in a div.
This is the first paragraph in another div.
This is the last paragraph in another div.// last method change the color of this p tag i.e last ( ) method
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)