Search code examples
jqueryhtmlvariablesappendmarkup

Is there possible let variable which have html markup have appendTo function in jquery?


first I have two variable:

        var $prev="<div>This is Hello World</div>"
        var $next="<h1>This is another Hello</h1>"

I want the $next's markup append to $prev's,just like

<div>This is Hello World<h1>This is another Hello</h1></div>

but I know the

$test.append($test1);

is impossible,but really?How can I have the same function?

I want html manipulate function could also be apply into variable that store html


Solution

  • do this :

    var $prev=$("<div>This is Hello World</div>");
    var $next=$("<h1>This is another Hello</h1>");
    

    this creates jQuery objects - then you can use the .appendTo() method.

    $next.appendTo($prev);