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
    Aug 2004
    Location
    schweiz
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question getElementById-prob

    I've searched your side a while and tried some things out, but nothing will work right.

    I've a
    js script in the head of the side:
    ----------------------------------------------------
    <script language =javascript>
    function newf(i)
    {
    var element = document.getElementById(i);
    element.style.bgcolor='red';
    }
    </script>
    ----------------------------------------------------
    and a php script that calls the function
    ----------------------------------------------------
    $newcolor ="newf";
    {echo "<td width = '18px'align = 'center' bgcolor = '$getit' id =$i name =$i javascript: onmouseover= $newcolor($i)>$dt.<td>";}
    }
    ---------------------------------------------------------------------
    but it's not working. The $i is an integer.

    do you see the mistake?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just a couple of things:
    In this PHP code, you've got braces when you don't need them and I dislike the string notation you've used (I like separating the variables from my strings..)
    PHP Code:
    $newcolor ="newf";
    echo 
    '<td width="18px" align="center" bgcolor="'.$getit.'" id="'.$i.'" name="'.$i.'" onmouseover="'.$newcolor.'('.$i.')">'.$dt.'.<td>'
    Since bgcolor is not a style property, you can't access bgcolor the way you tried. You have a couple of options:
    • Use element.bgcolor='red'; or element.setAttribute('bgcolor', 'red');
    • Stop using bgcolor and use background-color / backgroundColor


    Without using bgcolor:
    PHP Code:
    echo '<td width="18px" align="center" style="background-color:'.$getit.'" id="'.$i.'" name="'.$i.'" onmouseover="'.$newcolor.'('.$i.')">'.$dt.'.<td>'
    Code:
    <script language =javascript>
     function newf(i) {
      var element = document.getElementById(i);
      element.style.backgroundColor='red';
     }
    </script>
    Hope that helps/works!
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Aug 2004
    Location
    schweiz
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question still no action

    ok,the php code in the request worked fine. using the code above php was really not interested in first. now it likes it. I made it now like that, following your suggestion.
    <?...
    if...{
    for...{
    if...{...}
    else{
    $newcolor ="newf";
    echo '<td width="18px" align="center" style="background-color:'.$getit.'" id="'.$i.'" name="'.$i.'" onmouseover="'.$newcolor.'('.$i.')">'.$dt.'.<td>';}}}
    ...?>

    the prob is the js function. no style or bgcolor changes. but it is called as I can see when I do
    "Document.write("<input type = button value =i>")"
    , which opens a new side with that button.

    <script language =javascript>
    function newf(i)
    {
    //Document.write("<input type = button value =i>") to test
    var element = document.getElementById(i);
    element.setAttribute('bgcolor', 'red'); //nothing happens ?
    }
    </script>

  • #4
    New to the CF scene
    Join Date
    Aug 2004
    Location
    schweiz
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    that's good.
    now I'm happy again.

    var element = document.getElementById(i);
    element.style.backgroundColor='red';

    thank you so much sadiq

  • #5
    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
    You could use setAttribute() method either, but the attribute is bgColor, not bgcolor

    element.setAttribute('bgColor','red')
    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
    •