Search code examples
javascriptjqueryhtmlcssmargin-left

Select an element by it's margin-left


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.


Solution

  • You could use filter() to achieve this:

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

    Example fiddle