Skip to content Skip to sidebar Skip to footer

Jquery Can't Clone An Element Using Its Id

I am trying to clone a div using Jquery,then change one of its children's id and the add after another element: var s = $('#runwell1').clone().wrap('
'); s.find('#tag' +

Solution 1:

you have # in your id..

id="#runwell1" <---here

<div class="well well-large RunWell"id="#runwell1">
                          //-------------^----here

remove that # in the tag

and your id selector will work..

Solution 2:

You need also to change the div of the .clone(), to avoid double IDs

Try this:

var runNum = 1;
var s = $('#runwell' + runNum)
    .clone()
    .attr('id', '#runwell' + (++runNum))
    .wrap('<div>');

s.find('#tag' + runNum).attr('id', 'tag' + (++runNum));
$('#addrun').before(s);

Demo here

Solution 3:

var s = $('#runwell1').clone().wrap('<div>');

s will refer to what was in the selector WHEN IT WAS RUN. It will not contain what content has been manipulated in the DOM.

Post a Comment for "Jquery Can't Clone An Element Using Its Id"