user3638239 user3638239 - 5 months ago 48
jQuery Question

jQuery - filter on multiple conditions

I'm trying to build a filter based on multiple conditions, I would like to combine data and css properties.

Example:
I have multiple DIVs with a data attribute called "data-group" and among DIVs sharing the same data-group value, I would like to target DIVs that have relative positionning.

<div id="a" data-group="1" style="position:fixed">
<div id="b" data-group="1" style="position:relative">
<div id="c" data-group="1" style="position:fixed">
<div id="d" data-group="0" style="position:fixed">


I would like to create filter grouping all DIVs with data-group=1 and fixed position.

When I only have one condition, I tend to use the following:

common = $("div").filter(function() { return $(this).attr("data-group") === 1; });


...but how can I apply a second condition?

Thanks!

Laurent

Answer

Filter fixed:

common = $("div").filter(function() {  
  return $(this).attr("data-group") === 1 && $(this).css('position') === 'fixed';
});

Filter relative:

common = $("div").filter(function() {  
  return $(this).attr("data-group") === 1 && $(this).css('position') === 'relative';
});
Comments