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
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation some js help please

    Hi, I need some help changing a background color using javascript...
    The HTML Code has this structure:

    Code:
    <table>
      <table>
        <form>
          <tr>
            <td bgcolor="#000000"></td>
          </tr>
          <tr>
            <td bgcolor="#000000"></td>
          </tr>
          <tr>
            <td bgcolor="#000000"></td>
          </tr>
          .
          .      
          .
        </form>
      </table>
    </table>
    Now the tricky part is that I CANNOT alter the HTML code, I can't give the elements ID's or NAME's, is it still possible to change bgcolor via javascript?
    Can someone provide me a script template for changing all the td bgcolors to a specific color, like black?
    Last edited by sted; 04-23-2008 at 01:04 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    var tds=document.getElementsByTagName('td');
    
    for(var i=0;i<tds.length;i++)
        tds[i].style.backgroundColor='green';
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <table>
    <table>
    <form>
    <tr>
    <td id="cell1"></td>
    </tr>
    <tr>
    <td id="cell2"></td>
    </tr>
    <tr>
    <td id="cell3"></td>
    </tr>
    .
    .
    .
    </form>
    </table>
    </table>

    <script>
    function hilite(id) {

    document.getElementById("cell1").style.backgroundColor = "red";
    document.getElementById("cell2").style.backgroundColor = "black";
    document.getElementById("cell3").style.backgroundColor = "green";

    }
    </script>

    http://www.outsource-website-development.com/ http://www.outsource-website-design.com/

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <table>
    <table>
    <form>
    <tr>
    <td id="cell1"></td>
    </tr>
    <tr>
    <td id="cell2"></td>
    </tr>
    <tr>
    <td id="cell3"></td>
    </tr>
    .
    .
    .
    </form>
    </table>
    </table>

    <script>
    function hilite(id) {

    document.getElementById("cell1").style.backgroundColor = "red";
    document.getElementById("cell2").style.backgroundColor = "black";
    document.getElementById("cell3").style.backgroundColor = "green";

    }
    </script>

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This one should be correct

    <script language="javascript">

    function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload = function() {
    oldonload();
    func();
    }
    }
    }

    addLoadEvent(function() {
    var tds=document.getElementsByTagName('td');
    for(var i=0;i<tds.length;i++)
    tds[i].style.backgroundColor='green';
    });

    </script>

    <table>
    <table>
    <form>
    <tr>
    <td bgcolor="#000000"></td>
    </tr>
    <tr>
    <td bgcolor="#000000"></td>
    </tr>
    <tr>
    <td bgcolor="#000000">
    </td>
    </tr>
    </form>
    </table>
    </table>


  •  

    Posting Permissions

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