0

I have:

html:

<div>
  <a class="1"></a>
  <a class="2"></a>
  <a class="3"></a>
  <a class="4"></a>
</div>

js:

item = $('div a');

Which gives me an array of the 'a' elements. I want to add these elements again to the same array, so I tried:

item.push(item);

but what I ended up with is:

[a]
[a]
[a]
[a]
[[a],[a],[a],[a]]

instead of

[a]
[a]
[a]
[a]
[a]
[a]
[a]
[a]

What can I do to achieve this result?

7
  • 1
    You want to do what now? It's not an array, it's an array-like object, and it can't contain the same element multiple times. Commented Feb 3, 2014 at 20:46
  • 1
    what you are trying achieve... Commented Feb 3, 2014 at 20:46
  • 1
    jQuery uses $.unique behind the scene, so you can't do that. You should .get() the array and then push the duplicates. Commented Feb 3, 2014 at 20:49
  • 1
    @BlackSheep - Then the question becomes, why would you ever need an array with the same element multiple times ? Commented Feb 3, 2014 at 20:51
  • 1
    While answers have shown that it is possible to do this, you will probably run into problems later on. What exactly are you trying to achieve by that? Commented Feb 3, 2014 at 21:03

3 Answers 3

3

You could also try this:

edit: Forgot to add a working fiddle demo

item = $('div a');
$.merge(item,item);
Sign up to request clarification or add additional context in comments.

6 Comments

Yep . . . that's the one. I'm still curious as to what the point would be though. LOL
@adeneo, thanks i've updated my answer with a fiddle.
It's interesting that in documentation they don't state it should work with jquery objects
I'm surprised this works at all. I thought jQuery deduplicates the element set.
@Felix Kling: it doesn't have chance when this function is used. It modifies the collection from outside and doesn't notify it github.com/jquery/jquery/blob/master/src/core.js#L365
|
3

If you're looking to create an array with those elements, you could try this:

item = item.toArray();
item = item.concat(item);

Or this, which will work for arrays or jQuery objects:

item.push.apply(item, item);

14 Comments

Object [object Object] has no method 'concat'
How about you try it first, there is no jQuery concat method
@Matt The reason you are ending up with the first result is because you are adding [a, a, a] as an element...not as a set of individual variables. Concat will put those items together.
@zerkms / adeneo Oops, Corrected.
Concat is a base javascript function, it's not jQuery... w3schools.com/jsref/jsref_concat_array.asp
|
1

You need to go one by one

var itemsA = $('div a');

for(var i = 0; i<itemsA.length; i++){
 items.push(itemsA[i]);
}

What you were doing was pushing ALL the elements that $('div a') returned into one single position of items.

1 Comment

Note however if you do anything with this jquery object that causes a new jquery object to be created, the duplicates will be removed from the object.

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.