Rajeev
Rajeev

Reputation: 46949

insert div at a particular position

How to remove mynavbar and append to main_table as the first element

<div id="mynavbar" > <div>Some content</div>  </div>

<div id="main_table"> </div>

<script>
if(condtion == 0)
{
    // How to remove mynavbar and insert into main_table div as the first element
}
</script>

Upvotes: 0

Views: 982

Answers (5)

Vivek
Vivek

Reputation: 11028

for remove you can simply use...

$("#mynavbar").remove();

but once it will removed, it will not be there in your html..you should try something like this...

  var mynavbarHTML= $("#mynavbar").html();
   $("#mynavbar").remove();
  $("#main_table").prepend(mynavbarHTML);

Upvotes: 0

user1385191
user1385191

Reputation:

Here's a non-jQuery answer for some variety.

var main = document.getElementById("main_table");
var secondary = document.getElementById("mynavbar");
if(main.firstChild)
{
    main.insertBefore(secondary, main.firstChild);
    console.log("first");
}else
{
    main.appendChild(secondary);
    console.log("last");   
}

EDIT: I'm amazed that there's 5 nearly identical answers. Yikes.

Upvotes: 0

Bobo
Bobo

Reputation: 615

$('#main_table').prepend ( $('#mynavbar').html() );

$('#mynavbar').remove ();

Upvotes: 0

m3kh
m3kh

Reputation: 7941

$("#mynavbar").prependTo("#main_table");

Upvotes: 1

Zhasulan Berdibekov
Zhasulan Berdibekov

Reputation: 1087

$('#mynavbar').prependTo('#main_table').remove();

Upvotes: 0

Related Questions