Hello, I am new to JavaScript and could really use some assistance.
In the code snippet below, I am having trouble with the logic for the submit button. The line _btn.addEventListener
seems to be causing an issue where only the "if" part is being executed, and there seems to be a problem with the condition in the "else" part. Even when the date and image are clicked, only the alert from the "if" condition is being displayed.
<p align=center>
<input type="submit" value="submit" id="button">
</p>
<script type="text/javascript">
let _img = document.getElementById("img");
let _img1 = document.getElementById("img1");
let _img2 = document.getElementById("img2");
let _picker = document.getElementById("picker");
let _btn = document.getElementById("button");
let isImgClicked = false;
let isDatePicked = false;
_img.addEventListener("click", function(){
isImgClicked = true;
});
_picker.addEventListener("click", function(){
isDatePicked = true;
});
_btn.addEventListener("click", function(){
if(!isImgClicked || !isDatePicked) {
alert("Please select a year and click on the car image");
} else {
// logic for redirecting based on image and date selection
}
});
</script>