dcsimg

Assignment 17 - A Possible Answer

Select a Background Color

To get the effect, you need to insert a prompt before the function, then add the output of the prompt to the alert button. The full document looks like this:


<html>
<head>
<SCRIPT type="text/javascript">
  var user_name = 
  prompt 
    ("Write your name in the box below","Write it here");

    function newcolor(color)

    {
      alert("Hey " + user_name + "!  You Chose " + color)
      document.bgColor=color
    }
</SCRIPT>
</head>
<body bgcolor="white">
<center><h1>Select a Background Color</h1>
</center>
<form>
  <input type="button" 
    value="Blue" onClick="newcolor('lightblue')">
  <input type="button" 
    value="Pink" onClick="newcolor('pink')">
</form>
</body>
</html>

Close this window to return to JavaScript Primer