Link to home
Create AccountLog in
Avatar of DColin
DColinFlag for Thailand

asked on

Change image on click

Hi Experts,

The code below should toggle two images. When the page is run on the first click the image changes but it then does not change on any more clicks.

function ChangeImage(){

imageSrc = document.getElementById('ToDo2').getAttribute("src");
                    if (imageSrc == "/images/tickred.jpg") {
                        document.getElementById('ToDo2').setAttribute("src", "/Images/tickGreen.png");

                    if (imageSrc == "/images/tickgreen.png") {
                        document.getElementById('ToDo2').setAttribute("src", "/Images/tickRed.jpg");


I know one image is a jpg and one a png.
Avatar of Member_2_4694817

Check spelling: tickred.jpg or tickRed.jpg? tickgreen.png or tickGreen.png?
Avatar of DColin


Hi thehagman,

Changed everything to lower case and it is still doing the same. Image toggles for the first time only. I have change the images around and it does not matter which one is displayed first. It toggles once only.
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account