Yesterday I need to hide the last div element present on the webpage and I found that there are various jQuery ways to hide the last div element on the webpage. So thought of sharing...
Using .last() function
jQuery has ".last()" function which returns the last/final element from the set of matched elements.
Using .last() selector
jQuery also has ":last" selector which selects the last element out of set of matched elements.
Using .eq() function
jQuery has ".eq()" function which takes index as argument and returns the element at specified index. It can take positive value as well as negative value as index. When it takes positive value then it starts from the first element as 0-based position. But Providing a negative number indicates a position starting from the end of the set, rather than the beginning.
So to select last element, pass "-1" as index in ".eq()"
Using .last() function
jQuery has ".last()" function which returns the last/final element from the set of matched elements.
1 | $( "div" ).last().hide(); |
jQuery also has ":last" selector which selects the last element out of set of matched elements.
1 | $( "div:last" ).hide(); |
jQuery has ".eq()" function which takes index as argument and returns the element at specified index. It can take positive value as well as negative value as index. When it takes positive value then it starts from the first element as 0-based position. But Providing a negative number indicates a position starting from the end of the set, rather than the beginning.
So to select last element, pass "-1" as index in ".eq()"
1 | $( "div" ).eq(-1).hide(); |
Good Luck....
No comments:
Post a Comment