Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Dynamic Background Color Changer...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Dynamic Background Color Changer...

    Hello again folks ,

    For some nut-case reason this simple script has gone so wrong -

    <html>
    <body bgcolor="black">
    <input type="button" onclick="start()" value="Start">
    <script>
    function start() {
    var counter=1
    if (counter==1) {
    document.body.style.backgroundColor="brown"
    counter++}
    else if (counter==2) {
    document.body.style.backgroundColor="orange"
    counter++}
    else if (counter==3) {
    document.body.style.backgroundColor="blue"
    counter++}
    else if (counter==4) {
    document.body.style.backgroundColor="green"
    counter++}
    else if (counter==5) {
    document.body.style.backgroundColor="red"
    counter++}
    else {
    document.body.style.backgroundColor="black"
    counter=1}
    }
    </script>
    </body>
    </html>

    This script should be changing the background color every time the button is clicked but can't even change it once ! (I'm just seriously angry because I failed at such a simple script)...

    And some questions -

    1) Shoudn't I be able to edit any attribute of ANY tag by simple doing this for example - <input type="button" value="DOG" onclick="this.value='CAT'">

    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your function will never go farther than this:

    Code:
    function start() {
    var counter=1
    if (counter==1) {
    document.body.style.backgroundColor="brown"
    counter++}
    This is because every time it runs, counter is set to 1. Try:

    Code:
    var counter=1;
    function start() {
    if (counter==1) {
    document.body.style.backgroundColor="brown"
    counter++} . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    YES !!! - Thanks for your post John - Please also check out my other thread which is not being answered - Animation Issue (javascript forum index)

  4. #4
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Grrrrr, I was about to finish off my script with that edit but there is still something wrong ! -

    <html>
    <body>
    <input type="button" onclick="start()" value="Start">
    <script>
    var counter=1
    function start() {
    if (counter==1) {
    document.body.style.backgroundColor="brown"
    counter++}
    else if (counter==2) {
    document.body.style.backgroundColor="orange"
    counter++}
    else if (counter==3) {
    document.body.style.backgroundColor="blue"
    counter++}
    else if (counter==4) {
    document.body.style.backgroundColor="green"
    counter++}
    else if (counter==5) {
    document.body.style.backgroundColor="red"
    counter++}
    else {
    document.body.style.backgroundColor="black"
    counter=1}
    }
    </script>
    </body>
    </html>

    Did you catch anything ?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Your code works fine in Firefox. Test it out: http://phphost.smackum.com/test.html.
    Last edited by thetestingsite; 04-04-2007 at 06:17 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No dice

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    In Firefox, the code works fine; however in IE it doesn't work. I don't think this will work in IE:

    Code:
    document.body.style.backgroundColor='red';
    Not sure though. Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well I think I have used it before and it worked .... Do you mind downloading IE for a while and testing it out ...

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ok, I played around with it and found that IE doesn't like the button to have the onclick (at least in this script). So I changed it to a link and it works fine. View the test page (the link I posted above) to see for yourself. I will try to figure out how to get it to work with the button now.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I was just running a test before you said that and the same thing happend ! Except I used the onclick event on the body tag....

    What could possibly be wrong ????????

Bookmarks

Posting Permissions

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