Wednesday, August 25, 2021

jQuery pseudo selector Example

Apart from Class and ID selector, jQuery also provides CSS like pseudo selectors to select elements like first, last, even or odd in list of elements. For example, if you have an unordered list, which contains 5 list items and you want to select the first list item, you can use pseudo selector like $("#course li:first"). Similarly you can also choose the last list item in the unordered list by using $("#course li:last). Ifyou want to select alternate elements, you have options of choosing even or odd elements by using $("course li:odd) and $("#course li:even"). Odd selector will select all elements whose index is odd number, remember indes starts at 0. So odd selector will choose 2st and 4rd element and even selector will choose first, 3rd and 5th element. 


here is an example :

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.