0

js

var identbutton = document.getElementById('imagebutton');
var imag = document.getElementById('contentimage');
var runtext = function(){
    imag.style.background="url(../images/contimgtwo.jpg)";
}
identbutton.addEventListener("click",runtext,"false");

html

    <button id="imagebutton">click here to change images</button>
<img id="contentimage"></a>

css

#contentimage { 
display:block; 
background:url(../images/contimg.jpg);
top:1600px;
width:500px; 
height:400px; 
position:absolute;
} 

#contentimage:hover { 
    opacity:0.5; 
    cursor:crosshair;
}

I'm trying to make the image change when i press the button and so when you press the button the style of the image is suppose to change the image. This works and you can see the image change except all you see is a thin black border around the outline of the image and white space in the middle so you can't see the image. Also tried .className method instead of .style and didn't work

2
  • Can you create a jsfiddle.net? Commented Nov 24, 2013 at 5:52
  • here: jsfiddle.net/sKdR3 wierd, i changed the images to an external website and it found the second image when i clicked but not when it was server side, any idea why that might be? Commented Nov 24, 2013 at 6:16

1 Answer 1

1

You should be setting the image using the src attribute of the img tag instead of the background property. If you want to use background property, use a div tag instead of an img tag. Here is an example using the src attribute..

http://jsfiddle.net/2tSbq/

Essentially, its the same as what you were doing except I set the inital value of the image directly in the html with..

<img src="https://www.somewebsite.com/someimage.jpg" id="contentimage">

And then update it using javascript...

imag.src = 'http://www.somewesbtie.com/anotherimage.jpg";
Sign up to request clarification or add additional context in comments.

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.