swapping or moving divs

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

Suppose I have:

<div id="d1">
asdfas
</div>

<div id="d2">
asdfasf asdf
</div>

<input name="clickme" value="clickme" onclick="show_d1_in_div_2()"/>

What is code for the javascript function "show_d1_in_d2()" that
displays the contents of d1 in d2 when clickme is clicked?

Thanks.
 
I

Ivo

Suppose I have:

<div id="d1">
asdfas
</div>

<div id="d2">
asdfasf asdf
</div>

<input name="clickme" value="clickme" onclick="show_d1_in_div_2()"/>

What is code for the javascript function "show_d1_in_d2()" that
displays the contents of d1 in d2 when clickme is clicked?

You realize that you end with duplicate d1's, don't you, as your brief sais
nothing about hiding the original d1 and this example uses the cloneNode
method, which really does nothing other than clone the node. The
insertBefore method takes a second parameter allowing you to specify where
to insert the new element. If you replace "null" with "d2.firstChild", the
cloned node appears before the first child in d2. There is really nothing to
be surprised about here.

function show_d1_in_d2() {
var d1 = document.getElementById( 'd1' );
var d2 = document.getElementById( 'd2' );
d2.insertBefore( d1.cloneNode( true ), null );
}

You 're welcome.
ivo
http://www.yorick.onlyfools.com/
 
G

gimme_this_gimme_that

Thanks Ivo,

In my actual code I'm hiding a bunch of divs and then making one of
them visible at one spot on the page depending upon a onchange event.

I wanted to keep my example simple to get what I needed, which you
kindly provided:

d2.insertBefore( d1.cloneNode( true ), null );

Again Thanks.

Just what I needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top