I crafted this dropdown menu, but for some reason, the selections aren't registering when clicked. Below is the code I used. Any assistance would be greatly appreciated!

Hi there, I need some help with getting my code to run properly. I've created a dropdown box using HTML and CSS, but it seems like there's an issue with the JavaScript portion as the options are not being selected. I've included a code snippet below for reference. If you could take a look and provide me with some guidance, I would greatly appreciate it. Thank you in advance!

const select = document.querySelector(".select");
const optionsContainer = document.querySelector(".dropdown-list");

const optionsList = document.querySelectorAll(".dropdown-list_item");

.select.addEventListener("click", () =>{
   optionsContainer.classList.toggle("active");
})

optionsList.forEach( o =>{
  o.addEventListener("click",() =>{
    select.innerHTML= o.querySelector("label").innerHTML;
    optionsContainer.classList.remove("active");
  });
});
.dropdown{
  width: 20rem;
  position: relative;
  padding-left: 20%;
  color:white;
}

.dropdown:hover  .dropdown-list {
  opacity: 10;
  visibility: visible;
}

.dropdown-select{
  padding: 1.5rem;
  border-radius: 4px;
  background-color: black;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size:  1.6 rem;
  cursor: pointer;
}

.dropdown-list{
  border-radius: 4px;
  background-color: black;
  position: absolute;
  top:110%;
  left: 29%;
  right: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s linear, visibility 0.2s linear;
}

.dropdown-list_item{
  padding: 1rem;
  font-size: 1.4rem;
}
<div id="wrapper">
    <div id="textbox">
      <div id="content">
        <h1 class="form">COMPLAINT TYPE</h1>
</div>
<div class="dropdown">
  <div class="dropdown-select">
    <span class="select">Selected Type</span>
    <i class="fas fa-angle-down"></i>
    </div>
    <div class="dropdown-list">
      <div class="dropdown-list_item">Drainage System Issue</div>
      <div class="dropdown-list_item">Garbage Collection</div>
      <div class="dropdown-list_item">Public Place Cleanliness</div>
      <div class="dropdown-list_item">Sweeping Of Roads</div>
      <div class="dropdown-list_item">Others</div>
    </div>
  </div>

Answer №1

It seems that the leading . before the select.addEventListener(... may have been mistakenly added during a copy/paste action. Additionally, the error was occurring due to the use of

o.querySelector("label").innerHTML
where there is no corresponding label element in the provided HTML code. If you remove that line and simply use select.innerHTML = o.innerHTML;, it should achieve the desired functionality.

const select = document.querySelector(".select");
const optionsContainer = document.querySelector(".dropdown-list");

const optionsList = document.querySelectorAll(".dropdown-list_item");

select.addEventListener("click", () =>{
   optionsContainer.classList.toggle("active");
})

optionsList.forEach( o =>{
  o.addEventListener("click",() =>{
    select.innerHTML= o.innerHTML;
    optionsContainer.classList.remove("active");
  });
});
.dropdown{
  width: 20rem;
  position: relative;
  padding-left: 20%;
  color:white;
}

.dropdown:hover  .dropdown-list {
  opacity: 10;
  visibility: visible;
  cursor:pointer;
}

.dropdown-select{
  padding: 1.5rem;
  border-radius: 4px;
  background-color: black;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size:  1.6 rem;
  cursor: pointer;
}

.dropdown-list{
  border-radius: 4px;
  background-color: black;
  position: absolute;
  top:110%;
  left: 29%;
  right: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s linear, visibility 0.2s linear;
}

.dropdown-list_item{
  padding: 1rem;
  font-size: 1.4rem;
}
<div id="wrapper">
    <div id="textbox">
        <div id="content">
            <h1 class="form">COMPLAINT TYPE</h1>
        </div>

        <div class="dropdown">
            <div class="dropdown-select">
                <span class="select">Selected Type</span>
                <i class="fas fa-angle-down"></i>
            </div>
            <div class="dropdown-list">
                <div class="dropdown-list_item">Drainage System Issue</div>
                <div class="dropdown-list_item">Garbage Collection</div>
                <div class="dropdown-list_item">Public Place Cleanliness</div>
                <div class="dropdown-list_item">Sweeping Of Roads</div>
                <div class="dropdown-list_item">Others</div>
            </div>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A unique technique for creating a stunning visual effect with images using

Can anyone help me with this issue: Check out this animated GIF The images in my project are overlapping when scrolling! How can I achieve a similar effect for my images? Is there a tutorial or plugin available for this? Here is the current code sn ...

My AJAX requests do not include any custom headers being sent

I'm facing an issue with making an AJAX request from my client to my NodeJS/ExpressJS backend. After firing the request, my backend successfully receives it but fails to recognize the custom headers provided. For example: $.ajax({ type: " ...

Is there a way to use JQuery to make one button trigger distinct actions in two different divs?

For instance: Press a button - one div flies off the screen while another div flies in. Press the button again - Same div flies out, other div returns. I'm completely new to Javascript/JQuery so any assistance would be highly appreciated! Thank you ...

I keep encountering the issue where I receive the message "Unable to access property 'innerText' of an undefined element" when running the Array forEach function. This problem seems to be happening within the HTMLInputElement section of the code

I am facing an issue where the error occurs because "cardTxt" is not recognized as a string. I verified this using typeof syntax, but I'm unable to understand why it can't be a string. This code snippet includes the use of bootstrap for styling. ...

Using JQUERY to fadeIn elements when clicking on a button and loading content from an external HTML

On my webpage, when a user clicks on a navigation link, instead of changing to a new page, the content from the linked pages loads into a div on the main page using JQuery and .load function. Both tests worked perfectly with this implementation. Now, I wa ...

"Interact with jQuery by sliding side to side or in a circular motion

I am in need of assistance with sliding images using jQuery back and forth, or making them go round in a loop. Currently, the images slide up to the last element and then swiftly return to the first div, which is not visually appealing at all. I understa ...

Sending an image to a Kendo popup window triggers a [Request Uri too long 414] error message

On my webpage, I have a Kendo grid that is filled with images and a button labeled "Graph Map". https://i.stack.imgur.com/DyGVs.png Whenever the "Graph Map" button is clicked, a new window pops up displaying the image from the first record in the grid. By ...

The function screen.getByText is not available in this context

My experience with jest and react-testing-library has been smooth for the most part, but I encountered some challenges when transitioning to the screen > getByText/etc testing method. Test describe('test the dashboard when loaded', () => { ...

Emphasizing the date variable within a spreadsheet

Hey there! I'm struggling with the code below: <html> <head> <title>highlight date</title> <style> .row { background-color: Yellow; color:blue; } </style> <script type="text/javascript"> </script> &l ...

Transforming AJAX into jQuery

How can I rewrite the following code using only the jQuery library? <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> function do_it(value) { var url = "test.p ...

Effortless login authentication using Disqus through Google or Facebook tokens

I have set up my website to allow users to authenticate through Google and Facebook using passport, which uses the OAuth 2.0 API. Additionally, I am utilizing Disqus to manage the comments system on my site. However, I am running into a problem where user ...

Setting up Authorization for FETCH requests in NEXT.js using .env.local

`export default function reservations() { let [reservationStock, setReservationStock] = useState([]); useEffect(() => { fetch(url, { headers: new Headers({ Authorization: "MyBasic Credentials", " ...

Router DOM in conjunction with Next.js

I am attempting to extract the output of the code in navigation, but unfortunately it is generating a dreadful error: Unhandled Runtime Error Error: You cannot render a <Router> inside another <Router>. You should never have more than one in ...

Prevent overlapping of nodes and edges in a D3 force-directed layout

Check out this fascinating example at http://bl.ocks.org/mbostock/1747543: In the demonstration, Mike illustrates how to prevent nodes from colliding with each other in a graph. I'm curious if it's feasible to also prevent collisions between no ...

menu roll-up in an upward direction

Is there a way to implement the CSS effects mentioned in this article to make the menu roll up instead of down? Learn How to Create a Menu that Rolls Up Using CSS! ...

What is the best way to iterate through JavaScript objects within a Jade template?

Technologies such as Node.js, Jade, socket.io, jQuery, and JSON are being used in my project. In my "index.jade" file, I have the following code that receives "results" data as JSON from the backend: extends layout block content ul // more jade html h ...

How can I create a tooltip function in JavaScript/jQuery that uses the "this" keyword?

JSFiddle link: http://jsfiddle.net/lustre/awpnd6L1/1/ I am curious if it's possible to create a JavaScript function that consolidates the mouseenter code for a "More Info" tooltip. Rather than copying the code multiple times, is there a way to stream ...

Using JavaScript to inject PHP variables into multiple <span> elements

I have been searching for a similar query without any luck. Currently, I am attempting to display the number of Twitter/Facebook/RSS followers within a <span>....</span> in my sidebar. To retrieve the follower counts, I am using some PHP scrip ...

Receiving a blank array from the firestore database

I have written a code for the LeftBar Component where I am trying to retrieve data stored in the "contacts" document in Firebase. However, I am getting an empty array and I'm not sure why this is happening. Additionally, I would like to know how to ac ...

In JavaScript, the gallery feature with Lightbox effect creates a unique touch as only half of the screen fades to black in

Hello everyone, I'm a complete beginner when it comes to programming so please be gentle with me on my first question :) I'm trying to put together a simple website with a lightbox gallery, and you can check out what I have so far here. The prob ...