Valip
Valip

Reputation: 4610

Avoid duplicate code in jQuery

Is there any way to remove this duplicate code?

ldbShow.css({
    'left': 'auto',
    'top': 'auto'
})
ldbForum.css({
    'left': 'auto',
    'top': 'auto'
})

I was thinking about something like this:

(ldbForum, ldbShow).css({
    'left': 'auto',
    'top': 'auto'
})

Upvotes: 1

Views: 65

Answers (4)

Satpal
Satpal

Reputation: 133403

You can use .add() method

Create a new jQuery object with elements added to the set of matched elements.

ldbForum.add(ldbShow).css({
'left': 'auto',
'top': 'auto'
})

Upvotes: 0

Jai
Jai

Reputation: 74738

use .add() method:

ldbShow.add(ldbForum).css({
    'left': 'auto',
    'top': 'auto'
})

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use the add() method to join two or more jQuery objects together:

ldbShow.add(ldbForum).css({
    'left': 'auto',
    'top': 'auto'
});

Note that a better solution all together would be to use a common class on all the elements and then select them in a single jQuery object.

Upvotes: 0

DanielS
DanielS

Reputation: 774

You can declare a style as an object and reuse it.

var css = { 'left': 'auto', 'top': 'auto' };
ldbShow.css(css);
ldbForum.css(css);

Upvotes: 1

Related Questions