Dondell Batac Dondell Batac - 21 days ago 10
Javascript Question

JQuery append a div start tag only

Is there a way to insert only a div start tag like

$('#adiv').append("<div class='divtoinsert>'");


but it creates:
<div class='divtoinsert></div>
instead.

I want it to append only the start tag because in the middle, I am adding some content and I want it to insert an end tag when I have added all the content.

I am looking over the net a day now but unlucky enough to give me a hint.

Answer

Sure, just create a string variable to hold the open tag, concatenate your content to it inside a loop or whatever you want, then concatenate the closing tag. You can then append the variable to your element when finished.

Example:

var yourText = "<div>";

yourText += //whatever you want

yourText += "</div>";

$('#adiv').append(yourText);