Currently, I am attempting to develop a customized dropdown field and here is the code I have come up with:
const list = document.querySelector('.list');
const listItems = list.getElementsByTagName('p');
document.querySelector('#category').onclick = function() {
// I can display the .list DOM element
list.style.display = 'block';
}
Array.from(listItems)
.forEach(function(listItem) {
listItem.onclick = function() {
// Struggling to hide the .list DOM element
list.style.display = 'none';
// The value of .listItem is being outputted
console.log(listItem.getAttribute('value'));
}
}
);
<div class="dropdown" id="category" name="category">
<div class="trigger">
<p class="selected-category">Category</p>
</div>
<div class="list">
<p value="">None</p>
<p value="1">harum inventore</p>
<p value="2">dolorem voluptatem</p>
<p value="3">dolores consectetur</p>
<p value="4">velit culpa</p>
<p value="5">beatae nulla</p>
</div>
</div>
My challenge lies in switching the display
attribute of the list
from block
to none
. Despite my efforts, this task seems unachievable even after attempting it from two different segments in the code.
Being relatively new to JavaScript prompts me to seek assistance. I believe I may have missed something crucial or taken an incorrect route, although I tried multiple remedies without success. Therefore, I apologize for any inconvenience caused in advance.