Avatar of DColin
DColin
Flag 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.
JavaScriptAJAX

Avatar of undefined
Last Comment
Ovid Burke

8/22/2022 - Mon
Member_2_4694817

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

ASKER
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.
ASKER CERTIFIED SOLUTION
Ovid Burke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck