www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Change image

  1. #1
    Join Date
    Aug 2012
    Posts
    43

    resolved [RESOLVED] Change image

    Hi,

    Just new to JavaScript and I'm trying the following:

    I include my javascript file:
    HTML Code:
    <script type="text/javascript" src="javascript/script.js"></script>
    In my html code i have an image:
    HTML Code:
    <img id="nu" class="website" src="images/website_images/nu.png" />
    The script file contains the following:
    Code:
    var nu_image = document.getElementById("nu");
    
    function test() {
    window.alert('test!');
    };
    nu_image.onclick = test();
    Why isnt this working?

    When i use this it works:
    HTML Code:
    <img id="nu" class="website" src="images/website_images/nu.png" onclick="test();" />
    but i want the onclick etc in the javascript page, not between the html code...

    anyone any suggestions?

    Thanx!

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <img id="nu" class="website" src="images/website_images/nu.png" />
    <script type="text/javascript">
    /*<![CDATA[*/
    var nu_image = document.getElementById("nu");   // the image must exist before it is referenced
    
    function test() {
    window.alert('test!');
    };
    nu_image.onclick = test;  // no ()
    
    // use an anonomus function
    //nu_image.onclick = function(){ test();  }
    /*]]>*/
    </script></body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Aug 2012
    Posts
    43
    Vic,
    Thank you very much.
    I' had already been playing calling the function with or without ().

    But yes, i called the script before the image was created.

    Now i have put the link to the script at the end of the code and it works fine now!!!
    Glad to have it sorted out!

    Thanks!

    Is it an advantage to use a anonomus function ?

  4. #4
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66

    or even easyer...

    just enclose your script in a window.onload to be shure the image and it's id exist before loading the script.

  5. #5
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    Is it an advantage to use a anonomus function
    it is better practice
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles