thedeadlyone
thedeadlyone

Reputation: 59

jquery adding existing html element to var

Any idea how can i insert the main_nav to the exact place?

var header_top ="<header><div>";
var main_nav = $('#nav').html();
var header_bot = "</header></div>";

var final_header = header_top + main_nav + header_bot;

$('body').prepend(final_header);

Thanks in advance.

Upvotes: 0

Views: 150

Answers (3)

Barmar
Barmar

Reputation: 780798

I think this is what you want:

$("#nav").wrap("<header><div></div></header>");

This assumes that the #nav element is already positioned where you want it, so I didn't bother with the prepend. If you do need to move it to the beginning of the body:

$("#nav").wrap("<header><div></div></header>").prependTo($("body"));

Upvotes: 1

ayyp
ayyp

Reputation: 6598

If you're trying to have all of the code in one variable, I'd suggest doing something like

var header = "<header><div>"+$('#nav').html()+"</div></header>";
$('body').prepend(final_header);

Upvotes: 0

Igor
Igor

Reputation: 15893

You have the order of header and div opening and closing tags mixed up.

Upvotes: 2

Related Questions