Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,968
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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>

  • Users who have thanked xelawho for this post:

    cg_chinmay (05-26-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,968
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •