Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > swapping or moving divs

Reply
Thread Tools

swapping or moving divs

 
 
gimme_this_gimme_that@yahoo.com
Guest
Posts: n/a
 
      03-16-2007
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.

 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      03-16-2007
<(E-Mail Removed)> gave us
> 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 );
}

> Thanks.


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


 
Reply With Quote
 
 
 
 
gimme_this_gimme_that@yahoo.com
Guest
Posts: n/a
 
      03-16-2007
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
swapping divs dynamically -- my code too funky....;) maya Javascript 4 02-26-2008 11:19 PM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
swapping divs -- when divs NOT positioned absolutely.. maya Javascript 4 11-16-2007 05:11 PM
swapping divs... maya Javascript 4 11-15-2007 08:53 PM
Managing divs within divs.... rich HTML 0 02-02-2006 07:38 PM



Advertisments