views:

15

answers:

1

I want to get the selected element and then insert it's copies in few places.

var template = $("#info-" + country + " > .stats > .template").clone();
$(template).insertBefore("#info-" + country + " > .stats > .template");

What I'm doing wrong that it doesn't copy the element and insert it?

P.S. The element which I'm selecting to copy is display:none.

+1  A: 

You have an extra wrap there, template is already a jQuery object, you just need:

var template = $("#info-" + country + " > .stats > .template").clone();
template.insertBefore("#info-" + country + " > .stats > .template");

Or a bit simpler:

var template = $("#info-" + country + " > .stats > .template");
template.clone().insertBefore(template);

Or use .before() with a function, like this:

 $("#info-" + country + " > .stats > .template").before(function() { 
   return $(this).clone(); 
 });
Nick Craver
Hmmm, tried the 3rd example and nothing happened.. Live example: http://apps.starchat.ee/stats.php
Richards
@Richards - your selector doesn't match your markup, there's a `.map` in there, like this: `$("#instance-" + country + " > .content > .stats > .map > .template")`
Nick Craver
Bwaah, I didn't even pay attention to it- I thought it checks all children, doesn't it? Big thanks! :)
Richards
Nevermind, now it makes sense.. " > " is direct children only.. Ops.
Richards