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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macintosh Why doesn't this JavaScript code work?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
      <title>Image Changer</title>
      <style type="text/css">
    body {background-color:blue;}
      </style>
      <script type="JavaScript">
    function ImgChange()
    
    { if (document.getElementByID("QWERT").src=="img1")
    {document.(getElementByID("QWERT").src="img2"};
    else if( document.getElementByID("QWERT").src=="img2")
    {document.getElementByID("QWERT").src="img1"};
    }
      </script>
    </head>
    <body>
    <form> <input type="button" value="Change Image" onclick="ImgChange()"
     name="QwertButton"></form>
    <img src="img1" title="QWERT" alt="Image 1" id="QWERT">
    
    </body>
    </html>
    It's supposed totoggle an image when a button is pressed, but the image stays the same instead. And by the way, I tested the code on a 366 MHz iBook Clamshell on Classilla.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    {document.(getElementByID("QWERT").src="img2"};

    get rid of the ( before the getElement

    Also change all the ID to Id
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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