user3638239 user3638239 -5 years ago 380
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.

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?



Answer Source

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';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download