/    Sign up×
Community /Pin to ProfileBookmark

Change background color

I need to enter a color in a textbox. And when I press a button the background
changes to that color.

I know this is a very simple code so I hope someone can help me.

My teacher says I should use this code to do this.

var x
x=”red”
document.bgColor=x

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Ay__351_eFeb 22.2009 —  <br/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" onblur="document.bgColor=this.value"&gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;


<br/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt;
&lt;title&gt;&lt;/title&gt;

&lt;script type="text/javascript"&gt;

function Mitchman(x){
document.bgColor = x
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" onblur="Mitchman(this.value)"&gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MitchmanauthorFeb 22.2009 — Thanks Ayse,

Just one more quick question.

I can't have the color change until I press a button.
Copy linkTweet thisAlerts:
@Ay__351_eFeb 22.2009 — When the button is clicked, the color change.
<br/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt;
&lt;title&gt;&lt;/title&gt;

&lt;script type="text/javascript"&gt;

function Mitchman(){
var el = document.getElementById("mycolor");
document.bgColor = el.value
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" id="mycolor" &gt; &lt;input type="button" onclick="Mitchman()" &gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MitchmanauthorFeb 22.2009 — You did such a great job helping me with the last question maybe you could help me out with another on.

  • 1. Have the user type in the first box a number to start counting from.

  • 2. Have the user type in the second box a number where to stop counting to.

  • 3. Have the user type in a number to step up/down by when counting.


  • * When the user presses the button this page will clear and the list of numbers will be displayed.
  • * If the user uses the same starting and ending number alert them that is not valid.

  • * The user can enter a starting number bigger than the ending number.

  • * Take into account that a user might enter a positive or negative step value


  • I got some of it to work using Prompts instead of text boxes. I have been working on this all day and I can't get it to work. I no I am asking a lot can you please help me!!!!! Thank You In advance.


    <script type="text/javascript">

    x=prompt("enter start","")

    y=prompt("enter end","")

    x=parseInt(x)

    y=parseInt(y)

    for(I=x;I<=y;I=I+1){

    if(x==y){

    alert("The numbers are the same")

    }

    document.write(I)

    document.write("<br/>")

    }

    </script>

    </HEAD>

    <BODY>
    Copy linkTweet thisAlerts:
    @JMRKERFeb 23.2009 — Only need to check for duplicate entries once, not each time in the loop
    <i>
    </i>&lt;script type="text/javascript"&gt;
    x=parseInt(prompt("enter start",""));
    y=parseInt(prompt("enter end",""));
    if(x==y) { alert("The numbers are the same") }
    else {
    for(I=x;I&lt;=y;I=I+1) { document.write(I+"&lt;br/&gt;");
    }
    &lt;/script&gt;
    &lt;/HEAD&gt;
    &lt;BODY&gt;
    Copy linkTweet thisAlerts:
    @Ay__351_eFeb 23.2009 — Thanks JMRKER.


    &lt;script type="text/javascript"&gt;
    var x=parseInt(prompt("enter start",""));
    var y=parseInt(prompt("enter end",""));
    if(x==y) { alert("The numbers are the same") }
    else {
    for(var I=x;I&lt;=y;I=I+1) { document.write(I+"&lt;br/&gt;"); [COLOR="Red"]}[/COLOR]
    }
    &lt;/script&gt;
    &lt;/HEAD&gt;
    &lt;BODY&gt;
    Copy linkTweet thisAlerts:
    @JMRKERFeb 23.2009 — Oops ... you're right ... I typed too fast and didn't check.
    <i>
    </i> for(var I=x;I&lt;=y;I=I+1) { document.write(I+"&lt;br/&gt;"); [COLOR="Magenta"]}[/COLOR]
    ×

    Success!

    Help @Mitchman spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...