"Reference to Undefined Variable" say what?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    "Reference to Undefined Variable" say what?

    I was just playing around with javascript today and i wanted to change the style of a table element with javascript.

    The code i am using:
    [code=javascript]
    ChangeIt(id){
    var target = document.getEle mentById(id)
    var newColor = "#00ff33"
    target.style.co lor = newColor
    }
    [/code]

    The php (this is from withing a while loop):
    [php]
    echo("<tr bgcolor=".$bgco lor.">
    <td id=\"s".$counte r."\" onclick=\"Chang eIt('s".$counte r."');\">\n") ;
    echo $counter;
    $counter++; //increment counter
    echo("</td><td>\n");
    echo($row['url']);
    echo("</td><td>\n");
    echo($row['Util_Uploaded']);
    echo("</td></tr>\n");
    }
    [/php]

    I'm trying to get an onclick event where it catches the ID of the tag and then changes the style of said tag.

    I get this error though:
    Event thread: click
    Error:
    name: ReferenceError
    message: Statement on line 1: Reference to undefined variable: ChangeIt
    Backtrace:
    Line 1 of script
    ChangeIt("s2");
    At unknown location
    [statement source code not available]

    What am i doing wrong?

    Thanks.
  • Ferris
    New Member
    • Oct 2007
    • 101

    #2
    hi

    change your javascript code into:

    Code:
    function ChangeIt(id){
          	var target = document.getElementById(id);
          	var newColor = "#00ff33";
          	target.style.color = newColor;
    }
    hope it helps.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      How stupid am i!?!

      I would've stewed over that for hours, over a simple 'function'. I can't believe i missed that, haha.

      Thanks a bunch man :D

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        New problem:

        If i add some if and else statements in, i get the Reference to Undefined Variable ChangeIt

        [code=javascript]
        function ChangeIt(id){
        var target = document.getEle mentById(id)
        var newColor = "#00ff33"
        if(target.bgCol or != newColor){
        target.bgColor = newColor
        } else {
        target.bgColor = "#ffffff"
        }
        [/code]
        Problem solved, hah.
        Missed the last '}' out

        :)

        Comment

        Working...