views:

384

answers:

2

Hi,

I have two divs. I would like to move/populate the text from div id one to div id two using an onclick event. I am wondering how to do this? and also whether mootools can be used to accomplish the task or whether simple javascript is only necessary?

<script type="text/javascript" src="js/mootools.js"></script>

<script type="text/javascript">
alert($('side-a'));
function my_function(){
    var originalDivHtml = document.id('one').get('html');
    document.id('two').set('html', originalDivHtml);
}
</script>

<div id='one'>
 <ul>
  <input type="checkbox"  onclick = "my_function()"/>
  <li>some text 1</li>
  <input type="checkbox"  onclick = "my_function()"/>
  <li>some text 2</li>
 </ul>
<div>

<div id='two'>
<div>

I have also tried like this but it doesnt still work

<div id='two'>
 <ul>
  <input type="checkbox"  onclick = "my_function()"/>
  <li>some text 1</li>
  <input type="checkbox"  onclick = "my_function()"/>
  <li>some text 2</li>
 </ul>
<div>

<div id='one'>
<div>

<script type="text/javascript">
var one = document.getElementById('one'), two = document.getElementById('two');

function my_function () {
    while (two.childNodes.length) {
        one.appendChild(two.firstChild);
    }
}

</script>

Cheers in advance for any helps. Bangin my head against a brick wall here, because my javascript skillz are limited!

Ke

A: 
var originalDivHtml = $('one').get('html');
$('two').set('html', originalDivHtml);

or with hazelnut Javascript

var originalDivHtml = document.getElementById('one').innerHTML;
document.getElementById('two').innerHTML = originalDivHtml;

The function should be defined as:

function my_function() {
   var originalDivHtml = $('side-a').get('html');
   $('content').set('html', originalDivHtml);
}

You should have two elements having the ID's side-a and content for the above.

Anurag
hi, thx for the answer, is the first example mootools? also do i need to put it all inside a form, bz i cannot seem to get it to work?
Ke
yeap the first one is Javascript. the function is defined wrongly. what browser are you using? it's always helpful to look at the console logs to get a quick idea of what's wrong. it comes prebuilt with safari and chrome, and firebug is an awesome tool for firefox.
Anurag
yep, ive got ffox and firebug here, im getting an error with missing ; before statement. i have updated my code with the missing parantheses
Ke
cool, that got rid of the errors :) , although now i have a $ is not defined
Ke
check location of mootools.js, are you using any other libraries besides mootools?
Anurag
yep, it was the location which is now fixed, but now i get a $('one').get is not a function, strange, sorry to confuse things with side-a etc, was just trying to make things simple
Ke
cant get this to work, why does it say $('one').get is not a function, been looking round for ages and cannot find the answer
Ke
no worky at all
Ke
try document.id("one").get (if $ is defined already by another framework and you are on mootools 1.2.3+ it will revert back to document.id instead).otherwise, you have an issue of trying to use elements that are not extended, make _sure_ mootools actually loads and you run the code inside a domready callback
Dimitar Christoff
yes this doesnt work either, ive updated my code again and im 100% sure mootools is working bz ive tested it. now i get "document.id" is not a function
Ke
just wondering if someone can test my code, ive updated it many times already but the solution is none of the above. If you can test it that would be much appreciated, because i have tried each and every suggestion on here without any luck, cheers :)
Ke
this suggestion will work unless you use mootools 1.11/1.12 (which does not use .get), if so, check this fiddle: http://www.jsfiddle.net/tNrNq/ otherwise, for mootools 1.2+ -> http://www.jsfiddle.net/K2ceV/
Dimitar Christoff
A: 
var one = document.getElementById('one'), two = document.getElementById('two');

function move_two_to_one () {
    while (two.childNodes.length) {
        one.appendChild(two.firstChild);
    }
}
David Dorward
with this i get "two is null" cant work it out??
Ke
You are probably putting the script before the divs, so when the document.getElementId methods are called, the divs don't exist. Move the script so it is after the divs, or run the assignments onload.
David Dorward
this doesnt work no matter where i put the script, i do have the whole thing encased in one more div,but surely it should work if i have tried putting the script inside every div? is there something wrong with my html, e.g. needing to put each li inside a div?
Ke
Works for me. http://dorward.me.uk/tmp/move.html
David Dorward
Oh. You've edited your question. Look at the function name. It says "from_two_to_one". You have content in "one" and want to move it to "two", you need to swap the ids around.
David Dorward
yep, this example is slightly different to what im trying to do dorward.me.uk/tmp/move.html. Im tryin to make it so that each <li> gets added to the div as it is click, rather than adding them all at once. Is this possible to do?
Ke
An li cannot be a child element of a div, only of a ul or ol. Once you start using lists instead of divs, then it is quite possible. You just need to change the condition so that it goes until it has moved an element (noting that there will be white space nodes as well as element nodes in some browsers) instead of going until there are no elements left.
David Dorward