Question:

I want to use javascript to change the background colour within an IF statement?

by  |  earlier

0 LIKES UnLike

This is my code and its not working :(

<html>

<head>

<script LANGUAGE="JavaScript">

function main()

{

var favcolor, purple, blue, pink, yellow;

favcolor=prompt("Which colour do you prefer? purple, blue, pink or yellow?");

if(favcolor==purple)

{document.bgColor = '#CC00CC';}

if(favcolor==blue)

{document.bgColor = 'blue';}

if(favcolor==pink)

{document.bgColor = 'pink';}

if(favcolor==yellow)

{document.bgColor = 'yellow';}

document.write("You prefer "+favcolor+"? Cool!");

}

</script>

</head>

<body>

<center>

<script LANGUAGE="JavaScript">main();</script>

</body>

</html>

 Tags:

   Report

2 ANSWERS


  1. &lt;html&gt;

    &lt;head&gt;

    &lt;script LANGUAGE=&quot;JavaScript&quot;&gt;

    function main()

    {

    favcolor=prompt(&quot;Which colour do you prefer? purple, blue, pink or yellow?&quot;);

    if(favcolor==&#039;purple&#039;)

    {document.bgColor = &#039;#CC00CC&#039;;}

    if(favcolor==&#039;blue&#039;)

    {document.bgColor = &#039;blue&#039;;}

    if(favcolor==&#039;pink&#039;)

    {document.bgColor = &#039;pink&#039;;}

    if(favcolor==&#039;yellow&#039;)

    {document.bgColor = &#039;yellow&#039;;}

    document.write(&quot;You prefer &quot;+favcolor+&quot;? Cool!&quot;);

    }

    &lt;/script&gt;

    &lt;/head&gt;

    &lt;body bgcolor=&quot;pink&quot;&gt;

    &lt;center&gt;

    &lt;script LANGUAGE=&quot;JavaScript&quot;&gt;main();&lt;/script&gt;

    &lt;/body&gt;

    &lt;/html&gt;

    Why Ur previous code didn&#039;t work? Because statement if(favcolor==yellow) means there are variable yellow (like yellow=&quot;#ff0000&quot; or yellow = false), but u need to compare favcolor to string like &quot;yellow&quot;


  2. hey. the first person who answered has it right - when comparing favcolor to a string in your if statements, you need to put the string in inverted commas (ie if (favcolor == &#039;purple&#039;){//do this}. Otherwise, it thinks it is comparing to a variable called purple.

    your browser should recognise the name of most common colours, so you could actually adjust your program so that the user can type almost any colour and the background colour will change. see the code below:

    &lt;html&gt;

    &lt;head&gt;

    &lt;script LANGUAGE=&quot;JavaScript&quot;&gt;

    function main()

    {

    var favcolor;

    favcolor=prompt(&quot;What is your favourite color?&quot;);

    document.bgColor = favcolor;

    document.write(&quot;Your favourite is &quot;+favcolor+&quot;? Cool!&quot;);

    }

    &lt;/script&gt;

    &lt;/head&gt;

    &lt;body&gt;

    &lt;center&gt;

    &lt;script LANGUAGE=&quot;JavaScript&quot;&gt;main();&lt;/script&gt;

    &lt;/body&gt;

    &lt;/html&gt;

    almost any colour you can think of is covered here: orange, blue, black, white, grey, purple, pink, red, yellow, green, even things like darkblue and darkgreen, crimson, dodgerblue, aliceblue, springgreen....

    as long as there&#039;s no space between two words, that is!

    here&#039;s a list of all the named colours:

    http://www.gb.nrao.edu/~cbignell/testcol...

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions