i have this jquery script:
$('#add').click(function(){
$('<div class="drag" style="left:20px;"/>')
.text( num++ )
.appendTo( document.body )
.css({
top: $( window ).height() - 500 ,
left:$( window ).width() - 500
});
});
this will create: <div class="drag" style="left:20px;"/></div>
what i am interested in creating is this:
<div class="drag" style="left:20px;"/>
<div class="handle SE">
</div>
</div>
if i do this:
...
$('<div class="drag" style="left:20px;"/><div class="handle SE">')
.text( num++ )
...
jquery will create 2 separate di’vs and not nested.
any ideas?
thanks
Use
.wrap():Working example at http://jsfiddle.net/alnitak/r4Dz6/
In this particular example it’s neccesary to call
.appendTo(document.body)before the wrap is done because.wrap()returns the original element. Calling.appendTolast would result in the newly wrapped parent element getting removed from the hierarchy.