Moving text from one cell to another

M

Mike

Hello,
I am trying to move the contents of one cell to another with a click.
I have this in my table:
<td id="1" onclick="MoveTeam(this.id)" >Team 1</td>

and this in js:
function MoveTeam(id){
var x=document.getElementById(id).innerText;
document.getElementByID(21).innerText=x;
}

But it aint working.
Any suggestions
Thanks
Mike
 
K

Kailash Nadh

Hello,
I am trying to move the contents of one cell to another with a click.
I have this in my table:
<td id="1" onclick="MoveTeam(this.id)" >Team 1</td>

and this in js:
function MoveTeam(id){
var x=document.getElementById(id).innerText;
document.getElementByID(21).innerText=x;

}

But it aint working.
Any suggestions
Thanks
Mike

Notice the ids, t1, t2 and so on..
<td id="t1" onclick="MoveTeam(this.id)">Team 1</td>
<td id="t2" onclick="MoveTeam(this.id)">Team 2</td>
<td id="t3" onclick="MoveTeam(this.id)">Team 3</td>


If you click on t1, t1's content would be moved to t2 (+1 cell) and so
on.
Make sure you include some kind of check to determine when you're
clicking on the last cell (where there wouldn't exist a +1 cell)

<script type="text/javascript">
<!--
function MoveTeam(id){
var x = document.getElementById(id).innerHTML;
var next = parseInt(id.substring(1, id.length))+1;
document.getElementById('t' + next).innerHTML = x;
}
//-->
</script>

PS: I am not sure whether this is exactly what you want because clicks
apparently overwrite cells without storing anything.
 
E

Evertjan.

Kailash Nadh wrote on 27 nov 2007 in comp.lang.javascript:
Notice the ids, t1, t2 and so on..
<td id="t1" onclick="MoveTeam(this.id)">Team 1</td>
<td id="t2" onclick="MoveTeam(this.id)">Team 2</td>
<td id="t3" onclick="MoveTeam(this.id)">Team 3</td>


If you click on t1, t1's content would be moved to t2 (+1 cell) and so
on.
Make sure you include some kind of check to determine when you're
clicking on the last cell (where there wouldn't exist a +1 cell)

<script type="text/javascript">
<!--

not necesary in this century's browsrs
function MoveTeam(id){
var x = document.getElementById(id).innerHTML;
var next = parseInt(id.substring(1, id.length))+1;
document.getElementById('t' + next).innerHTML = x;
}
//-->
same

</script>

Do you want this perhaps:

====================================
<table onclick='MoveTeams(this);'>
<tr>
<td>Team 1</td>
<td>Team 2</td>
<td>Team 3</td>
<td>Team 4</td>
<td>Team 5</td>
<td>Team 6</td>
</tr>
</table>


<script type='text/javascript'>

function MoveTeams(t){
var i = t.cells.length-1;
var temp = t.cells.innerHTML;
while (i>0) {
t.cells.innerHTML = t.cells[i-1].innerHTML;
i--;
};
t.cells[0].innerHTML = temp;
};

</script>
====================================
 
K

Kailash Nadh

Kailash Nadh wrote on 27 nov 2007 in comp.lang.javascript:




not necesary in this century's browsrs

Yep, this is more efficient. I was only trying to retain the poster's
idea of having ids for each cell.
function MoveTeam(id){
var x = document.getElementById(id).innerHTML;
var next = parseInt(id.substring(1, id.length))+1;
document.getElementById('t' + next).innerHTML = x;
}
//-->
same

</script>

Do you want this perhaps:

====================================
<table onclick='MoveTeams(this);'>
<tr>
<td>Team 1</td>
<td>Team 2</td>
<td>Team 3</td>
<td>Team 4</td>
<td>Team 5</td>
<td>Team 6</td>
</tr>
</table>

<script type='text/javascript'>

function MoveTeams(t){
var i = t.cells.length-1;
var temp = t.cells.innerHTML;
while (i>0) {
t.cells.innerHTML = t.cells[i-1].innerHTML;
i--;
};
t.cells[0].innerHTML = temp;

};

</script>
====================================
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top