LiveEn
LiveEn

Reputation: 3253

remove existing div and append to new div on load using jquery

i have a set of div's

<div id="foo">foo</div>
<div id="q"></div>
<div id="bar">bar</div>
<div id="tags">tags</div>

i need to remove <div id="tags">tags</div> and append it to <div id="q"></div> when the page is loaded using jquery.

i tried the below but doesnt work

jQuery(function ($) {

 $( '#tags' ).ready(function() {
        $('#q').append(this);

    });


});

instead of ready if y try click it works but i need to show it when the page is loaded

any help will be appreciated

Upvotes: 0

Views: 1039

Answers (2)

talhatahir
talhatahir

Reputation: 85

$( document ).ready(function() {
     var ele= $('#tags').html();
     $('#tags').remove();
     $('#q').append(ele);
 });

Upvotes: 0

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : You can use appendTo() or append() as shown below

1) appendTo()

$(function() {
   $('#tags').appendTo('#q');
});

JSFiddle Demo

2) append()

$(function() {
   $('#q').append($('#tags'));
});

JSfiddle DEMO

NOTE - make sure that ids of the elements are unique through out the DOM

Upvotes: 2

Related Questions