2

I am trying to select element which has been cloned

$("THINGS TO CLONE").clone().appendTo("CONTAINER");

And it should have looked

> <CONTAINER>
> 
> <CLONED THING> <CLONED THING> <CLONED THING>
> 
> </CONTAINER

I made it but these things are invisible for selecting.

roflol
  • 637
  • 2
  • 7
  • 9
  • Is it that since you added the elements after the dom was ready, you have to bind the new elements to jQuery? – auo May 02 '12 at 08:43
  • If you are trying to select the cloned elements by `id` you will only get the original elements, beacuse ids must be unique. Also, you should store the cloned elements in a variable so you retain a reference to them. – Rory McCrossan May 02 '12 at 08:45

1 Answers1

9

That doesn't make sense. If you've cloned your elements correctly, and inserted them into the DOM somehow, they're perfectly selectable using jQuery.

If you want to select your cloned elements, however, one way you can go about it is caching the original in-memory cloned object before slapping it into your DOM.

var clone = $('#foo').clone();

// do stuff with clone
clone.doBackflips();

// append it to the DOM
clone.appendTo('#container');
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
  • This is so obvious, I've seen this many times, but the way you structured it is a little bit easier on the head. Thanks – Tatarin Feb 10 '14 at 20:49
  • Is it possible to select items inside a cloned element before appending it? I think it is, and I want to prevent it. – Jeff Aug 06 '15 at 23:11
  • @Jeff ~ cloned elements behave just like any other jQuery collection --- the only difference is they're "detached" (i.e. not visible in the DOM just yet), at least right after cloning. So yeah, you can select elements from cloned elements, perform processing on them --- anything you can do with jQuery elements really --- before you actually perform the `.append()`. – Richard Neil Ilagan Aug 10 '15 at 01:29
  • That's what it was. I didn't know you could select elements not attached. – Jeff Aug 10 '15 at 01:31