I'm in the process of developing a calendar feature where users can select a specific "day" element to access a dropdown menu for time selection. The functionality is working fine, but I've encountered an issue. When a user selects a time from the dropdown menu, it also triggers the parent "day" element, creating another dropdown menu with different times upon initial selection. How can I implement a JavaScript if statement that specifically detects clicks on child elements and not parent elements?
JavaScript:
function create_dropdown(day){
var insertdiv = document.createElement('select');
insertdiv.className = "times";;
day.appendChild(insertdiv);
for(var i=0;i<times_full.length;i++) {
insertdiv.options[insertdiv.options.length] = new Option(times_full[i], i);
}
}
document.querySelectorAll(".day").forEach(day => {
day.addEventListener("click", event => {
event.currentTarget.classList.toggle("selected");
if(event.currentTarget.classList.contains('selected'))
{
day.style.backgroundColor='#B0E0E6';
create_dropdown(day);
}
else
{
day.style.backgroundColor='transparent';
}
});
});
var times_full = [
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
];
An illustration of these elements can be seen here: