Phil
Phil

Reputation: 14671

jQuery: Increase padding top by +x pxs

With jQuery, how can I find all locations where a specific padding-top value is applied and then increase it from its existing value?

I looked at selectors and I could not find such a thing.

Upvotes: 3

Views: 2147

Answers (3)

calebds
calebds

Reputation: 26228

Do you really mean to search the whole DOM? It's quite inefficient:

$('*').each(function() {

    var padTop = $(this).css('padding-top'); // get the padding
    if (padTop > 0)
        $(this).css('padding-top', padTop + x); // add x to it if more than 0

});

It seems like each() is better than filter() because we can filter by padding and increase it in the same scope. It's strongly adviseable to replace '*' with the most specific selector you can afford to use.

Upvotes: 2

sazh
sazh

Reputation: 1832

Are you trying to apply this only to a particular type of element (ex: divs only?). In that case you could do something like @Jeroen mentioned:

$('div').filter(function() { return this.style.paddingTop == "2px"; }) // use your specific padding value.

Upvotes: 1

Jeroen
Jeroen

Reputation: 63790

You can use a filter with a function that checks for a specific padding on elements?

Might be a tad slow statement though...

Upvotes: 1

Related Questions