0

I'm wondering if it's possible to do something like this in jQuery:

$( [ $selector1, $selector2, document.getElemetByID( 'test' ) ] )

EDIT: I'm lazy. $selector1 and $selector2 are already jQuery objects. I am trying to avoid using $.each.

Here's a more in-depth example of what I'm trying to do:

var $lastLink = $( '.links' ).filter( ':last' );
var $nextThumbnail = $( '.thumbnail.current' ).next();

Let's say I want to add a class of active to those things and I would like to keep it as simple as possible - therefore I am looking for something with similar syntax to what I posted before.

$( [ $lastLink, $nextThumbnail ] ).addClass( 'active' );
7
  • 1
    What is it that you are trying to do with that? What are you hoping for as the result? Commented Mar 6, 2015 at 22:11
  • $("selector1", "selector2", document.getElementById("test")) comma delimit for multiple selectors Commented Mar 6, 2015 at 22:12
  • @SterlingArcher - Maybe . .. or he could be looking for $("selector1, selector2, #test")). Hard to tell, based on how he's written it. Commented Mar 6, 2015 at 22:14
  • 1
    @SterlingArcher The first argument will be a selector and the second one the context. Commented Mar 6, 2015 at 22:14
  • The specifics of the selector don't really matter, but I saw it as multiple selectors. That's why I didn't post as answer lol Commented Mar 6, 2015 at 22:15

3 Answers 3

1

EDIT:

Yes, but not exactly like that. If each of your $selector variables represents a collection of a single DOM element, you can rewrite your code like this:

$([$selector1[0], $selector2[0], document.getElementByID('test')])

And you will be able to add a class to all of those elements at once without .each()

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

3 Comments

Yeah, but I still need to use $.each - I wanted to specifically avoid this.
I guess you've got a little typo. The opening square bracket is missing after $(. Great answer, though!
@IfediOkonkwo Thanks for pointing that out, missed it in my haste to type
1

I don't believe it supports it within a single function call.

But, you could chain calls to .add():

$().add($selector1).add($selector2).add(document.getElementById('test'))

When given an Array, jQuery() generally expects the contents to already be Elements. Otherwise, it treats it as using the jQuery(object) overload, leaving the strings as is.

console.log( $(['#foo', '#bar']).get() ); // [ '#foo', '#bar' ]

1 Comment

While this is not really what I'm looking for, it's pretty close. Thanks.
0

I think the short answer is "no". JQuery does not expect array as selector.

Its either a comma-separated string list, or a single DOM object or a jQuery object.

A second argument is treated as the "parent" container within which to conduct the search.

If you insist on collecting several objects into one, an option is to do this:

$($selector1).add( $selector2).add(document.getElemetByID( 'test' ))

Comments

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.