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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Location
    U.K.
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy cell bgcolor on checkbox status with onload and onclick

    ok heres the problem

    the page has several checkboxes each in its own table in its own cell.

    when the page LOADS i would like the colour of the cell the checkbox is in to depend on the status of the checkbox.

    For example if the checkbox is not checked then the cell will be green if it is checked then the cell will be red. the status of the checkbox will be from a database and php will say whether the box is checked or not.

    i believe something on the lines of this is needed however if your style different or you are not happy the please re-code the below

    NOTE : THE CODE BELOW DOES NOT WORK

    <script type="text/javascript">
    //<![CDATA[
    onload = function()
    {
    for(i=0; i<document.getElementsByTagName('input'); i++)
    {
    if(document.getElementsByTagName('input')[i].type == 'checkbox')
    {
    var c = document.getElementsByTagName('input')[i];
    c.parentNode.parentNode.style.backgroundColor = (c.checked) ? '#f00' : '#090';
    }
    }
    }
    //]]>
    </script>

    NOTE : THE CODE ABOVE DOES NOT WORK


    i would like the same thing to happen for onclick

    <input type="checkbox" onclick="parentNode.parentNode.style.backgroundColor = (checked) ? '#f00' : '#090';" />

    i belive that the above is needed to do this and the code above DOES work.

    i hope this is clear as my javascript knowledge is limited

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not just have the PHP script set the color while it's also marking whether the box is checked or not?
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    i<document.getElementsByTagName('input').lenght
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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