Passing arguments to filter function in JQuery

JQuery allow to use function as .filter() argument to test each element in the set

$( "li" )
  .filter(function( index ) {
    return (index > 3) && (index < 10);
  })
.css( "background-color", "red" );

how I can to pass arguments to this function so it looks like

$( "li" )
  .filter(function( index, min, max ) {
    return (index > min) && (index < max);
  })
.css( "background-color", "red" );

Upvotes: 2

Views: 2400

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30557

Declaring the variables before calling filter should work just fine

var min = 3, max = 19;
$( "li" )
  .filter(function( index ) {
    return (index > min) && (index < max);
  })
.css( "background-color", "red" );

There is no need to pass in arguments in this case.

Upvotes: 8

Related Questions