tags:

views:

23

answers:

2

Hi folks, hope can help, trying out Jquery clone which seems to work, but I get "multiple" clones not single clones on "repressing" the button.

e.g.: I want to clone this :

echo '<select class="hello">';
    foreach ($pageposts as $post):
    echo '<option>'.$post->post_title.'</option>';
    endforeach;
    echo '</select>';

on click of this

echo '<input type="button" id="rp" value="add">';

Yes from WordPress and Yes the "hello" class is from the JQuery pages

My JQuery function is:

$j=jQuery.noConflict();
$j(document).ready(function() {
$j('#rp').click(function(){ 
$j('.hello').clone().appendTo('#goodbye');
});
});

So my "overall code snippit" looks like this:

echo '<select class="hello">';
foreach ($pageposts as $post):
echo '<option>'.$post->post_title.'</option>';
endforeach;
echo '</select>';
echo '<div id="goodbye"></div>';
echo '<input type="button" id="rp" value="add">';

I clone "once" on the first press, but then it goes in multiples i.e.:

1 click gives 1 clone plus 1 original - what I want

2 clicks gives 3 clones plus 1 original - not what I want I want 1 original plus 2

3 clicks gives 7 clones plus 1 original - not what I want I want 1 original plus 3 etc.

Suggestions please. Thanks

A: 

It's because your selector is looking for a class:

$j('.hello')

Everytime you clone and append this to another element you're adding yet another .hello element, therefore your cloning every single .hello element it can find.

Perhaps you should remove the class name when it's been cloned:

$j('.hello').clone().removeClass('hello').appendTo('#goodbye');

Or perhaps even change it:

$j('.hello').clone().removeClass('hello').addClass('cloned_hello').appendTo('#goodbye');

You might want to add a different class so your CSS will still work, but ultimately this is why you're getting multiple cloned items.

ILMV
Thanks ILMV - Durrrr of course I'm missing the obvious as usual.
No problem russp, looking over your user history your accept rate needs rectifying... accept this answer if it's helped :-)
ILMV
Thanks again ILMV - your obviously a quicker typer than I am, you beat me to the .removeClass('hello') aspect. Many thanks, problem solved
ILMV - just a quick note I do accept "Answers" - sometimes I find "Answers" are not Answers - (unlike yours) Just can't press the button for a few mins. yet!
:D No problem dude, just saying.
ILMV
A: 

Hi,

the behaviour is normal, because you clone every element that has the hello class in it.

Try this :

$j('.hello').clone().attr("class","cloned").appendTo('#goodbye');

And you put in your css the same in .cloned as in .hello .

Hope it helps :)

Michael Lumbroso
You shouldn't use the method `.attr` to add/edit/remove classes from an attribute, as you can have multiple classes your method would overwrite others. Instead use `.addClass` and `.removeClass` :-)
ILMV
Here there is only one class so it doesn't overwrite anything, but I agree in general it's not the best!
Michael Lumbroso