SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript timer error

    hi,

    I have been playing around with JavaScript a i can't seem to get the code below to work properly.
    If I give backgroundColor(letter) a number between 0-9 it works fine but if I give it a-z it does not
    work. All get in the in the console unexpected token, the alert shows the right letter I have pressed

    any ideas?
    Code:
    function backgroundColor(letter){
    	alert(letter);
    	  document.getElementById(letter).style.color = "orange"; 
    	  
    	  window.setTimeout("backgroundReturn("+letter+")", 250);
    	  
    }
    /*returns the keybroad to white after keypress*/
    function backgroundReturn(letter){
    	
    	 document.getElementById(letter).style.color = "white";
    	
    }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    II would have expected the results to be the other way around since it isn't valid to start an id with a number.

    You could try fixing up the setTimeout to pass a function as is expected rather than a string and see if that helps.

    setTimeout(function() {backgroundReturn(letter);}, 250);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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