benhowdle89
benhowdle89

Reputation: 37474

jQuery multiple selectors but cached in a variable

I'm trying to avoid the following:

var dynS = $('#dyn-style');
var cssA = $('#css-active');

$('#selectors li').click(function(){
    switch($(this).attr('data-id')){
        case 'first-of-type':
            dynS.empty().text('#target p:first-of-type {color: red;}');
            cssA.empty().text('#target p:first-of-type {color: red;}');

Is there any way I could use the cached selector variables and set the text() as the same on both, to avoid this duplication?

Upvotes: 1

Views: 152

Answers (2)

gen_Eric
gen_Eric

Reputation: 227270

Use .add

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Try using .add see below,

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');

Upvotes: 3

Related Questions