Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  176] [ 6]  / answers: 1 / hits: 27412  / 9 Years ago, sat, december 5, 2015, 12:00:00
<!DOCTYPE html>
<html>
<body>

<img id = imageOne src = circleRed.png onclick = changeColor()/>

<script>
var image = document.getElementById(imageOne);

function changeColor() {
if (image.src == circleRed.png) {
image.src = circleBlue.png;
} else {
image.src = circleRed.png;
}
}
</script>
</body>
</html>


This whole program may seem to work but no. I'll just be able to change the color of my image once. After clicking for the second time, nothing happens. What I mean is that I can only change the color from Red to Blue.
Could you please help me figure out why?


More From » html

 Answers
181

Here is the solution:



<!DOCTYPE html>
<html>
<body>

<img id =imageOne src =circleRed.png onclick = changeColor()/>

<script>
var image = document.getElementById(imageOne);

function changeColor()
{
if (image.getAttribute('src') == circleRed.png)
{
image.src = circleBlue.png;
}
else
{
image.src = circleRed.png;
}
}
</script>
</body>
</html>

[#64156] Wednesday, December 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Fri, Oct 16, 20, 00:00, 4 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Sun, Jul 28, 19, 00:00, 5 Years ago
;