The "change" event listener is not responding in the browser
// mobile options
const selectElement = document.querySelector(select)
let spices = document.querySelector(".spices")
let milletAndPorridges = document.querySelectorAll(".millet")
let healthSupplements = document.querySelectorAll(".health")
let spicesProducts = document.querySelectorAll("")
let milletProducts = document.querySelectorAll("")
let healthSupplementProducts = document.querySelectorAll("")
selectElement.addEventListener('change', function(event) {
const selectedValue = event.target.value
if (selectedValue === spices) {
// this is just for testing purposes
alert('SPice')
}
})
<div class="sorting">
<select name="" class="">
<option value="default" class="default">Sort by Category</option>
<option value="default" class="spices">Spices</option>
<option value="sort-by-price" class="millets">Porridges & Millets</option>
<option value="sort-by-popularity" class="health">Health Supplements</option>
</select>
</div>
I tried the above javascript which didn’t work, I also tried using a forEach loop on every option and adding a click listener to each option which didn’t work also.
3
Answers
bro, in your first line maybe you miss surround the string select.
Clean up the event listener then decide how to use the value.
Here I added a class just to be clear.
HTML changes
The changes in JS
Here are the changes that you need to do.
Upvote the answer if you found it helpful.