www.webdeveloper.com
Results 1 to 2 of 2

Thread: Having trouble replacing an images' source?

Hybrid View

  1. #1
    Join Date
    Jun 2014
    Posts
    21

    Question Having trouble replacing an images' source?

    Hi, so I wanted to make an image change when clicked, seemed simple enough, but when I run the page and click the image- nothing happens. Worse still, I think I understand what's going on, which means I clearly don't if it isn't working!

    Here is my HTML:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <!-- References the external stylesheet -->
    <link rel="stylesheet" type="text/css" href="design.css">
    <title>SECTION - WELCOME</title>
    </head>
    
    <body>
        <div> 
    	<h1>WELCOME</h1>
        <section> 
    	<!--Central Image-->
    	<img src="./images/button_unpressed.jpg" alt="FORBIDDEN WEBSITE" id="displayed" onclick="imageChange(./images/button_pressed.jpg)"/>
    	    <script type = "text/javascript" src="functionality.js"></script>
    	
    	    
    	</section>
        </div>
    </body>
    
    </html>
    Here is my Javascript:

    Code:
    imageChange(sourceVar)
    {
    document.getElementById("displayed").src = sourceVar;
    
    
    }
    Thank you to anyone who helps

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,498
    You need to declare the function with the use of

    Code:
    function functionName( paramaters ){
        any actions to take...
    }
    What you have is

    Code:
    functionName( paramaters ){
        any actions to take...
    }
    then...

    Your onclick event for the image
    Code:
    onclick="imageChange(./images/button_pressed.jpg)"
    has not been properly formatted

    Try to "enclose" the inner text in single quotes so you don't break the double quotes.
    Code:
    onclick="imageChange('./images/button_pressed.jpg')"
    Now in future you should use the web browsers console, most can be accessed with a press of F12 and see what errors are listed...
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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