What's wrong with my html/javascript code below? Thanks. When i click to change text color it freezes...?

<html><head><title>Javascript tests </title> <script type="text/javascript"> function cbcolor(){ var bcolor=prompt('what color would you like the background?') document.bgColor=bcolor } function ctcolor(){ var color=prompt('what color would you like the text?') document.write('<font color=' + color + '>') } </script> </head> <body bgcolor=gray> <script type="text/javascript"> document.write("Hi <br>") alert('this is an alert') confirm('confirm box') var response=prompt('What is Steven?','fetter sheissesel') document.write('<marquee scrollamount=7 scrolldelay=5 behavior=alternate>Steven is a ' + response +'</font></marquee>' ) </script> <p align=right><form> <input type='button' value='Click to Change Bg color' onclick='cbcolor()'><br> <input type='button' value='Click to Change Text color' onclick='ctcolor()'> </form></p> </font> </body></html>

Public Comments

  1. Not sure but for one thing, you shouldn't be using the <font> tag. It's no longer being supported. You need to use CSS. Speaking of CSS, this can be accomplished a lot easier using the 'document.getElementByID()' method.
  2. You are misusing document.write() rather horribly. That's what's wrong. Essentially what you were doing were making it so that <font color=' + color + '> was the only html active on the page, and nulifying everything else. Everything else was erased, and since all you wrote to the page was the font tag it would end up being a blank white page with nothing else on it. To fix the problem just change: document.write('<font color=' + color + '>') to document.fgColor=bcolor I still don't approve of everything you are doing with this code- but this will work now.
Powered by Yahoo! Answers