Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-26-2012, 03:44 PM   PM User | #1
cg_chinmay
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
cg_chinmay is an unknown quantity at this point
Question Swapping table cells backgroundcolor

I created a table with one row and two cells.I am trying to swap the bgcolors of the two cells on button click ,here's my code:
[CODE]
<table>
<tr>
<td bgcolor="red">cell1</td>
<td bgcolor="blue">cell2</td>
</tr>
</table>
<input type="button" value="swap" onclick="swap()"/>
<script type="text/javascript">
var x=document.getElementsByTagName('td');
var temp=new array();
function swap()
{
temp[0]=x[0];
temp[0].style.backgroundColor=x[0].style.backgroundColor;
x[0].style.backgroundColor=x[1].style.backgroundColor;
x[1].style.backgroundColor=temp[0].style.backgroundColor;
}
</script>
</body>
</html>
[ICODE]
But this isn't producing any output on button click,how to implement this?
cg_chinmay is offline   Reply With Quote
Old 05-26-2012, 05:15 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
there is no need for an array (which should be written as new Array, btw) and you are not setting the bgcolor by the style attribute so you can't get it that way either. Simplified:
Code:
<script type="text/javascript">
var x=document.getElementsByTagName('td');
function swap(){
temp=x[0].bgColor;
x[0].bgColor=x[1].bgColor;
x[1].bgColor=temp;
}
</script>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
cg_chinmay (05-26-2012)
Old 05-26-2012, 05:44 PM   PM User | #3
cg_chinmay
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
cg_chinmay is an unknown quantity at this point
Thanks a lot..
In this case there is just 1 cell getting stored in temp,but when there are more than 1 we must use array so I tried temp=new array(3);
temp[0]=x[0].bgColor and so on but it doesnt give output so eventually i had to use 3 different variables,where m i wrong?

Last edited by cg_chinmay; 05-26-2012 at 06:32 PM..
cg_chinmay is offline   Reply With Quote
Old 05-27-2012, 09:22 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Quote:
an array (which should be written as new Array, btw)
Javascript is case sensitive. With "new array" you will never get an array. It's
Code:
var temp = new Array();
// or
var temp = [];

Last edited by devnull69; 05-27-2012 at 09:27 AM..
devnull69 is offline   Reply With Quote
Old 05-27-2012, 06:44 PM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if you just want to swap between two colors you can do this, regardless of how many tds there are:

Code:
<script type="text/javascript">
var tds=document.getElementsByTagName('td');
function swap(){
for (var i = 0; i < tds.length; i++) {
tds[i].bgColor=tds[i].bgColor=="blue"?"red":"blue"
	}
}	
</script>
xelawho is offline   Reply With Quote
Reply

Bookmarks

Tags
dom, swapping colors, table cells

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.