3

So basically I have some div's and I need to select just the ones that don't have a margin-left set, or it's set to 0px.

Is there any way I can achieve this with jQuery ?

<div class="col col-xs-12"></div>
<div class="col col-xs-12" style="margin-left: 0px;"></div>
<div class="col col-xs-12" style="margin-left: 10px;"></div>
<div class="col col-xs-12" style="margin-left: 20px;"></div>

In this example I should select the first 2 div's because both of them have a margin-left of 0px (the first is not set but it's still 0px);

PS. I prefer not to use a for for this operation.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
paulalexandru
  • 9,218
  • 7
  • 66
  • 94
  • `$whatYouWant = $('.col').filter(function() { return $(this).css('marginLeft') == '0px'; });` – billyonecan Jan 21 '16 at 08:40
  • Selecting elements by their margin-left value is a rather unusual request – can you explain what you want to achieve by this in the end? (Maybe there are other solutions.) – CBroe Jan 21 '16 at 08:58
  • I want to create a custom bootstrap carousel, because the actual one don't gives the posibility to display 4 items on xs, 3 items on md, 2 items on sm and 1 item on xs and in the same time to be responsive. – paulalexandru Jan 21 '16 at 09:01

1 Answers1

4

You could use filter() to achieve this:

$('.col').filter(function() {
    return parseInt($(this).css('margin-left'), 10) == 0;
});

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339