Sunday, 27 March 2016

How to filter elements using jQuery ?


Filtering elements using jQuery

Basic filtering methods allows you to select a specific element based on its position in a group of elements.
Other filtering methods, like filter() and not() allow you to select elements that match, or not match a certain criteria.

first() and last()
Returns the first or last element of the selected elements.
Example  Selects the first p element inside the first div element
$(document).ready(function() {
  $("div p").first();
});

Example  Selects the last p element inside the last div element
$(document).ready(function() {
  $("div p").last();
});


eq()
Returns an element with a specific index number of the selected elements. (index start at 0 )
Example  Selects the second p element having index number 1
$(document).ready(function() {
  $("p").eq(1);
});


filter()
Filters based on criteria.
Elements that do not match the criteria are removed from the selection and those that match will be returned.
Example  Returns all p elements with class name 'intro'
$(document).ready(function() {
  $("p").filter(".intro");
});


not()
Returns all elements that do not match the criteria. (It is the opposite of filter)
Example  Returns all p elements that do not have class name 'intro'
$(document).ready(function() {
  $("p").not(".intro");
});

No comments:

Post a Comment

Note: only a member of this blog may post a comment.