1

Is is possible to concatenate two jQuery selectors without creating a new selector in the process? An example would be as follows:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
        <span class="a">A</span>
        <span class="a">B</span>
        <span class="b">C</span>
        <script language="JavaScript" type="text/javascript">
            <!--
                var a = $('.a');
                var b = $('.b');
                var c = a.add(b);

                console.log(a);
                console.log(b);
                console.log(c);
            //-->
        </script>
    </body>
</html>

Which yields the following selectors when written as above:

a = [ A, B ]
b = [ C ]
c = [ A, B, C ]

I would like to modify it to produce:

a = [ A, B, C ]
b = [ C ]
c = whatever

Without creating a new selector in the process, the equivalent of using an Array's "push" method on each element in the selector.

2
  • You can also redefine the variable without creating a new one: a = a.add(b); (don't add var to the beginning) Now a will be [A,B,C] but you lose its initial setting so you don't have a variable that just calls [A,B] Commented Mar 27, 2013 at 15:22
  • I am aware you can redefine the variable. I am looking to concatenate the selectors without the overhead of creating a new instance of a selector. Commented Mar 27, 2013 at 15:35

3 Answers 3

2

You can use multiple selectors in jQuery:

$('.a, .b')

The above selector will select all elements that have the class .a and/or .b

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

3 Comments

I'm trying to append to a selector derived from a separate function at different points in the execution of the page, that is not an option as the original selector statement is not known.
No, in the example I provided it would be appending the items (zero or more) in "b" into the selector "a" without creating a new instance of a selector (using the "a.add(b)" creates a new selector instance, it does not append to the existing instance contained in the variable "a").
1

You can use internal hack method push:

a.push.apply(a, b.get());  // it accepts only native DOM elements

But I wouldn't suggest you to use it, since it is not documented and provided for internal use only. Instead use simple: a = a.add(b).

REF: https://stackoverflow.com/a/14345461/1249581

7 Comments

Each selector may or may not contain more than 1 item. I am trying to find a method that doesn't involve writing a loop every time I want to do the join or creating a new selector instance with the overhead that implies (I asked specifically to get around the overhead of .add).
@CoryG I still can't understand what do you want. push() method mutates the source jQuery object, exactly as you described.
Yes, but not for all objects in the selector being appended, each selector can attain 0 or more objects.
@CoryG Ah yeah, now I see. Look at my updated solution, it will append all elements from jQuery set.
get() returns DOM elements out of jQuery object, which is the only possible argument for push() here. To filter for duplicates you can use $.unique() method, which is internally used by add().
|
1

You can use multiple selectors in a jQuery string simply by comma-separating them, like so:

$('.a, .b')

1 Comment

I'm trying to append to a selector derived from a separate function at different points in the execution of the page, that is not an option as the original selector statement is not known.

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.