Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector - style values

I got a series of divs like this:

<div class="message" style="padding-left: 0px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>
<div class="message" style="padding-left: 40px;">...</div>
<div class="message" style="padding-left: 20px;">...</div>

And I would like to make a selector that would get me the divs with padding greater then 20px.

Would it be possible with just using jquery? Or I should modify my html tree and add some attribute that would distinguish those elemenents with high padding value?

like image 802
kender Avatar asked Aug 23 '26 19:08

kender


2 Answers

You can use a filter with a custom function.

$('div.message').filter(function(){
     return parseInt($(this).css('padding-left')) > 20;
});

p.s. I don't sure what .css('padding') > 20 will return, I'm guess I need to test it....

like image 79
Fitzchak Yitzchaki Avatar answered Aug 26 '26 11:08

Fitzchak Yitzchaki


You can use filter for this.

var elems = $('div.message').filter(function (){
               return parseInt($(this).css("padding-left"),10) > 20;
            });

alert ( elems.length );

or using each you can do something like this

$(function(){
    var elems = new Array();
    $("div.message").each(function(){
        if(parseInt($(this).css("paddingLeft"), 10) > 20 )
        {
            elems.push($(this));
        }           
    });

    alert ( elems.length );
});
like image 42
rahul Avatar answered Aug 26 '26 10:08

rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!