dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Setting style using javascript

  1. #1
    Join Date
    Oct 2008
    Posts
    13

    Setting style using javascript

    Hi,

    I've got this, struggling to get it working correctly

    <script type="text/javascript">
    function highlight(col,color)
    {
    for(i=1; i<=100; i++)
    document.getElementById(col+i).style.backgroundColor="#000";
    }
    </script>

    This is an example of a table cell

    <td id="colOne<%#Container.DataItem("rowIndex")%>" onmouseover="highlight('colOne','yellow')" onmouseout="highlight('colOne','white')">&pound;<%#Container.DataItem("s2year10knotmaintainedCH")%></td>

    When I hover over the cell nothing happens, what did I do wrong?

    Thanks

  2. #2
    Join Date
    Apr 2008
    Posts
    157
    You're not specifying which color you want to apply to the element.
    You need to use the functions as follows:
    function highlight(col,color) {
    for(i=1; i<=100; i++) document.getElementById(col+i).style.backgroundColor=color;
    }

    And then you'll want to pass HEX Color Codes onto the function, like this:
    onmouseover="highlight('colOne', '#fff')" would be white.
    You can look up the color codes here: http://www.addedbytes.com/cheat-shee...t-sheet-v1.png

  3. #3
    Join Date
    Oct 2008
    Posts
    13
    Thanks for that, i've changed the code as you suggested, but it still doesn't work, nothing happens when I hover over those cells.

    Thanks

  4. #4
    Join Date
    Apr 2008
    Posts
    157
    Can you give the link to your site?

  5. #5
    Join Date
    Oct 2008
    Posts
    13
    I managed to fix this, there was a typo in one of my cell id's

    But, now its thrown up a couple of new problems.

    For the rows of my column i'm using the <tr class="mycssclass"> to set the row style using css, this lets me highlight rows a specific colour depending on the cell values etc.

    But my onmouseout code breaks the css. Is there anyway in the onmouseout event I can set the cell to the backcolor it was before the onmouseover event?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles