Making select options that are distinct from each other

I have developed a dropdown feature that requires users to click on specific text in order to reveal more content below. Here is the code for this functionality:

.dropbtn {
  cursor: pointer;
  font-weight: bold;
}

.dropdown03 {
  position: relative;
  display: inline-block;
  text-align: left;
  width: 100%;
}

.dropdown-content03 {
  display: none;
  position: relative;
  width: 100%;
  overflow: auto;
  z-index: 1;
}

.show {display: block;} 
 <script>
  /* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content03");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script> 
<div class="dropdown03">
  <button onclick="myFunction()" class="dropbtn">Dropdown Button Text</button>
  <div id="myDropdown" class="dropdown-content03">
  Dropdown Button Text
    </div>
</div>

This code works perfectly on my website. However, I encountered an issue when trying to add another dropdown using the same code. The first dropdown displays correctly, but subsequent dropdowns do not work as intended - they only show the content of the first dropdown. (I hope this explanation makes sense)

Is there a way to ensure that each dropdown shows its own content when clicked? Do I need to create unique tags for each individual dropdown?

Answer №1

Updating both the IDs and class names can make a significant difference. Give it a try and don't forget to check the console by using the inspect tool. Review your HTML code under the inspect tab to ensure there are no issues showing up in the console.

Remember to perform a hard refresh of the browser by pressing Ctrl+F5 when reloading the page.

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

What causes padding to appear when adjusting the image size within a CSS grid layout?

Currently, I am working on my portfolio and have a projects section. In this section, I have implemented an image CSS grid that is functional but the images appear to be too large https://i.sstatic.net/KNHqJ.png. I would like to make them smaller, however, ...

A guide to updating a particular row and sending parameters with jQuery and AJAX

I am utilizing a JSON response to dynamically display table content. Here is the code I am using to display row values: var rows = ''; for(var i=0; i<response.response.length; i++) { rows += '<tr><td class="country">&ap ...

Is there a way to utilize JavaScript in order to trigger a random audio file to play upon clicking?

Is there a way to create a button using the div element that, upon being clicked, plays a random audio file from a set of options? I found some helpful discussions on this topic here and here. Based on those resources, I created a script utilizing Math.ra ...

Is it possible to create dynamic animations with SVG files?

I am currently in the process of coding my website, and I came up with an exciting idea to animate my navigation bar within an SVG container. With its naturally wavy design, I aim to achieve a sweeping wave effect similar to the intro animation on Discord. ...

I am having trouble with my :hover selector being interrupted by text, what steps should I take to resolve this

My issue revolves around an image with a hover effect that changes the box shadow color. However, there is text on the image that also changes when clicked, creating a problem where the area occupied by the text prevents the box shadow from displaying prop ...

The table is spilling over the edge of the parent container only in Firefox. This issue is not related to padding, margins

My table is causing an issue by overflowing the width of its container. It displays fine in Chrome, but Firefox is giving me trouble. If you'd like to see the site for yourself, it's available here: http://goo.gl/vkN5s I attempted using box-siz ...

What is the best way to position a div to float or hover from the bottom after it has been

I am working on creating a menu where clicking it will reveal a submenu at the bottom, but I'm encountering an issue. Currently, in my code, the submenu is appearing from right to left before moving down. Here is my code: <meta name="viewport" co ...

Is it true that when "text-align: center;" is applied to an element with absolute positioning, it has no effect on its child elements?

Let me show you the code first before explaining my issue: Tinkerbin: <style> div.container{ height: 200px; width: 200px; background-color:red; margin-top: 10px; } div.subContainer{ position: relative; text-align: ce ...

Transferring data from local storage to a PHP server using AJAX

My attempt to transfer data from local storage to PHP using AJAX resulted in an error mentioning 'undefined index data'. chart.php <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="t ...

javascript function to divide text into separate elements

Currently, I am facing an issue with my front end where a string of words (item names) fetched from the database is being displayed in a loop without any separation. The output appears as: Item name1Item name2 This problem arises due to passing the value ...

Adjusting the template layout of Django Rest Framework

Modifying the login function in the code is my current task - {% block userlinks %} {% if user.is_authenticated %} {% optional_logout request user %} {% else %} {% optional_login request %} {% endif %} {% endblock %} I am loo ...

Initiate a CSS animation by defining a series of keyframes, followed by seamlessly transitioning into another set of keyframes to

Is it possible to create an animation that starts with one CSS keyframe set and then transitions into looping another keyframe set indefinitely? ...

Capture video frames from a webcam using HTML and implement them in OPENCV with Python

While many may find it simple, I am facing a challenge in extracting video frames from HTML or JavaScript and utilizing them in my Python OPENCV project. I have a strong background in Python OPENCV and Deeplearning, but lack knowledge in HTML and JavaScr ...

"Is there a way to initiate a WebKit animation once the webpage has finished loading

My header wrapper has an animation that I want to start only after the page has completely loaded. Unfortunately, it seems like the animation starts before the page finishes loading. I found a function on CSS Tricks that I am currently using here. The is ...

Comparison of button text alignment: stacking vs inline placement

I currently have a button on my webpage labeled "Learn More." Unfortunately, the text on the button is not lining up as it should. Instead of flowing in a line, the text seems to be stacking on top of each other. The HTML code for the button is included ...

When an image is a child of the parent content and another element has opacity set, the CSS stack order will be altered

Recently, I came across an interesting issue while working with CSS, particularly with negative margins to stack elements on top of each other. I am aware of the natural stack order in which elements overlap, where the later element always appears on top ...

Switching the Vue image on Routerlink's isExactActive feature

Is there a way to display different images based on whether a link is active or not? <router-link to="/"> <img src=" :active = isExactActive ? pic_active.png : pic_inactive.png}}" /> //Something along these lines ...

What is the best way to store the input names and values from a webpage into an object using javascript?

My goal is to save the names and values of inputs on a page into an object. Each input name should serve as the key in the object, with its corresponding value being the value of the input. How can I achieve this? I simply need to store the input names an ...

htmlEnhance your webpage with a native-like combobox

<!DOCTYPE html> <html> <body> <form action="demo_form.asp" method="get"> <input list="browsers" name="browser" placeholder="choose browser"> <datalist id="browsers"> <option value="Internet Explorer"> ...

when an element is hovered over, it activates a reaction on a different element

I've been struggling with the Events assignment we were given. I tried writing my own code, but it had so many errors when I got it checked. I couldn't quite grasp all the explanations of what went wrong, so now I feel a bit embarrassed to share ...