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
2010-06-15 15:15:07
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
2010-06-15 15:19:36
Nick, good point. I was thinking class but typing ID. Updated and simplified the answer accordingly.
Ken Redler
2010-06-15 15:27:50
+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
2010-06-15 15:22:21