I have just started coding javascript in notepad++ at a GCSE level. One of my tasks in my Assessment is to make traffic lights change colour, one image for each button click. I have got as far as Making the Red light change to a Red-Yellow light. However, I am unable to go further with this. My code so far:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function lewis() {
document.getElementById("pic").src="Red-Yellow.png"
}
</script>
</head>
<body>
<button type="button" onclick="lewis()">
Change Lights</button>
<img src="Red.png" alt="Red" id="pic" style="width:250px;height:600px;">
</body>
</html>
This is the height of my ability as I have just started. Any help would be appreciated, many thanks, Lewis
(I will continue to do research alongside this question)
Didn't tested but i guess you want something like that.
function lewis() {
var current_image = document.getElementById("pic").src;
if(current_image == "Red.png"){
document.getElementById("pic").src = "Red-Yellow.png";
}else if(current_image == "Red-Yellow.png"){
document.getElementById("pic").src = "Red-Green.png";
}else if(current_image == "Red-Green.png"){
document.getElementById("pic").src = "last-color.png";
}else if(current_image == "last-color.png"){
document.getElementById("pic").src = "Red.png";
}
}