What are different mode of filtration in jQuery | Filter Methods

0
(0)

What are different mode of filtration in jQuery | Filter Methods

 

Find Elements by Index
Every child element starts its index from zero,
selector.eq( index )
index is the position of the element in the set of matched elements, starting at 0 and going to length – 1.
So to access List item no 2, use $(“li”).eq(1)

 

Filtering out Elements
The filter( selector ) method can be used to filter out matched elements out of all elements
The selector can be a class selector, id selector or other selector

selector.filter( selector )
selector can be a comma-separated list of expressions to apply multiple filters at once as filter(“.class1, .class2”)).

select listing item having class active
$(“li”).filter(“.active”).addClass(“highlight”);

 

Checks the current selection against an expression.
Returns true, if at least one element of the selection fits the given selector.
Return false, If no element fits, or the selector is not valid,

element.is( selector )

$("li").click(function () {
   if ($(this).is(":first-child")) {
      $("p").text("This is list item 1");
   } else if ($(this).is(".middle0,.middle1")) {
      $("p").text("This is middle class list");
   } else if ($(this).is(":contains('item 5')")) {
      $("p").text("It's 5th list");
   }
});

 

Build lists of values, perform special, custom, selector transformations.
Translate a set of elements in the jQuery object into another set of values in a jQuery array (which may, or may not contain elements).

selector.map( callback )

selector.map( callback )
  var listitems = $("li").map(function (index) {
  var listitem = $("<li>").text($(this).text()).get(0);
  if (index == 0) {   
    $(listitem).text($(listitem).text().toUpperCase());
  } else if (index == 1 || index == 3) {
    listitem = null;
  } else if (index == 2) {
    listitem = [listitem,$("<li>").get(0)];
    $(listitem[0]).append("<b> - A</b>");
    $(listitem[1]).append("Extra <b> - B</b>");
  }
  return listitem;
});
$("#altered_result").append(listitems);

 

Removes an elements 
Removes elements matching the specified selector from the set of matched elements.
selector.not( selector )
selector can be comma-separated list of selectors to apply multiple filters at once (e.g.

not(".class1, .class2")).
$("li").not(".middle").addClass("highlight");

 

Selects a subset of the matched elements.
slice( start, [end] )
selector.slice( start, end )
Parameters:
start − Where to start the subset. The first element is at zero. Can be negative to start from the end of the selection.
end − Where to end the subset excluding end element. If unspecified, ends at the end of the selection.

$("li").slice(2, 5).addClass("highlight");

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

1 Trackback / Pingback

  1. jQuery Interview Questions - etechstuffs

Leave a Reply

Your email address will not be published.


*


two + nineteen =