0

Sorry to sound naive since I'm a beginner. I have been trying to load an image on my website with the image hosted somewhere else online and having a url "http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg"

I have an image tag defined as below

<img id="image" src="" alt="" />

Moreover my javascript function executes the following on a button click

document.getElementById("image").src="http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg";

The URL seems to work fine, but the image doesn't show up onclick.

Am I making any mistake?

Thanks for any suggestion :)

2
  • Are you sure the correct function triggers on-click? Commented Sep 4, 2015 at 17:21
  • Yes I checked that :( Commented Sep 5, 2015 at 4:51

2 Answers 2

1

I could see that you are using " inside here, may be the HTML would also have " so that, it might not work. You can use the following code:

<a href="#"
   onclick='document.getElementById("image").src="http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg"; return false'>Change</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick respond. I checked it but that isn't the issue unfortunately :(
0

I created a Jsfiddle, and it seems to work fine. https://jsfiddle.net/ltlombardi/jvts4m3y/

var coco = function(){
document.getElementById("image").src="http://www.vapor-rage.com/wp-content/uploads/2016/02/Vapor-Rage-Small-Logo-new.png";
};
<img id="image" src="" alt="" />
<a href="javascript:void(0);" onclick="coco()">Click here</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.