3

Suppose, I have multiple selectors used

$('.some, .next, .any, .way, .myname, .something')

and now if I would like to change each background when one background is changed, so I'm wondering how can I iterate all selectors using likely to this: $(this).each() but I know this will not work coz this will take currently selected each elements but I would like to do all the selectors in my selectors used.

0

1 Answer 1

3

If you really need to do it :

var o = $('.some, .next, .any, .way, .myname, .something');
$.each(o.selector.split(/,\s*/), function(s){
    var os = o.filter(s); // or $(s)
    ...
});

Not that this simple solution wouldn't work for any selector, a parsing would be needed if you really want to support them all (but it's probable you don't have commas in your elements id ;) ).

Sign up to request clarification or add additional context in comments.

2 Comments

n can u explain about filter(s) , i din't understand that...!
In the loop, s is a simple selector. You may indifferently use $(s) or o.filter(s). In case of simple selectors based on classes or id, $(s) would be faster. If the selectors are heavy and the elements few, using o.filter(s) would avoid a global search, by looking only in o.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.