#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Set/read backgroundcolor ?


    Hi all,
    I have a html table and use jscode to set background color of some table cells like:

    document.getElementById("myid").style.backgroundcolor = #B1DFED;

    but when I read the backgroundcolor like:

    alert(document.getElementById("mytable").rows[1].cells[1].style.backgroundColor.toUppercase());

    I get: RGB(177, 223, 237)

    ???
    Hot to get the hexvalue I used to set the color ???

    TIA
    acki4711
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    218
    Rep Power
    32
    Hello acki4711,
    I'm sure there're other ways more efficient.

    Following is an untested one, with no error-checking:
    PHP Code:
    function hexColorOf(element){
      var 
    ret '#',
        
    arr element.style.backgroundColor.slice(4,-1).split(',');
      for (var 
    i=0i<arr.lengthi++)
        
    ret += Number(arr[i]).toString(16);
      return 
    ret;

    So to get the color of #mytable at row 1 column 1, you can call
    PHP Code:
    hexColorOf(document.getElementById("mytable").rows[1].cells[1]) 
    and it should return a string e.g. '#b1dfed'

IMN logo majestic logo threadwatch logo seochat tools logo