Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript button to change background colors

    I am new to JavaScript. As a student we are given projects and I cannot for the life of me figure this one out. I am to use the a simple document object, specifically document.bgColor, to create one button that directs the user to click to change background color. I can change color fine but not to change the color back. The rub here is only to use one button and to keep it simple. If anyone can help me I would be eternally grateful. Thank You in advance
    Last edited by hull; 03-09-2005 at 08:53 PM. Reason: wrong document object

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    can u show us what you have done so far? (we usualy don't do other's homework, but we might help you to correct it )
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I can show you what I have here at work I have some code at home that does not execute either. First I have this that works only one click.
    now I tried to add some var but it did not execute. so I have left what does work and I feel I am just missing something. To correct an error It is document.bgColor sorry about that.

    <HTML>
    <head>
    <title>color change once</title>
    <script language="JavaScript">
    <!--
    function newbg (difcolor)
    {
    document.bgColor=difcolor;
    }
    //-->
    </script>
    </head>
    <body bgColor="yellow">
    <b>Stop looking at this horrible color! Click the button, Change the color.</b>
    <p>
    <input type="button" value="Change Color" OnClick="newbg('lightblue');">

    </body>
    </HTML>

    Then I tried this:

    <HTML>
    <head>
    <title>background Colors</title>
    <script language="JavaScript">
    <!--
    function switchcolor()
    {
    if (document.bgColor=="yellow")
    {
    document.bgColor="lightblue";
    }
    else
    {
    if (document.bgColor=="lightblue")
    {
    document.bgColor="yellow";
    }
    }
    }
    //-->
    </script>
    </head>
    <body bgColor="yellow">
    <b>Want a new background color? Click the Button!</b>
    <p>
    <form>
    <input type="button" value="Change Color" onClick="switchcolor();">
    </form>

    </body>
    </HTML>

    and this does not work at all. This is an intro course so I want to keep it simple. I am really just looking for suggestions as to what direction to move. I am new to JS and Forums too so any help is appreciated.

    Hull


  •  

    Posting Permissions

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