tags:

views:

34

answers:

2

I'm trying to do the following: Get a DIV that is inside a hidden div in the page and put it next to another element via jquery. Is there any simple way to do that?

A: 

Here's one approach.

sourceDiv = $('div#your_hidden_div').find('div#the_other_div');
sourceDiv.insertAfter('div#the_other_div');

Edit: Updated to reflect Nick's valid comments.

Ken Redler
This would require the same ID being used twice in the page, so wouldn't work really...no need to clone, just `.insertAfter()` to *move* it.
Nick Craver
Nick, good point. I was thinking class but typing ID. Updated and simplified the answer accordingly.
Ken Redler
+1  A: 

Let's suppose that this is your html:

    <div class="hidden-div">
        <div id="MyDiv"> I want to move it </div>
    </div>
    <div id="target">
        <a href="#" class="crazyLink"> link </a>
    </div>

And your js should look like:

$(document).ready(function(){
    $('#MyDiv').insertAfter('#target a :last') ;
});

This would put "MyDiv" next to the crazyLink anchor

Matias