The drop-down box options on the web page are not visible to me, but I am able to see them in the element

Having a peculiar issue with two dropdowns on my webpage. The first dropdown works perfectly fine, but when I click on the second dropdown, the options don't show up on the webpage, even though they are visible in the element window. Both dropdowns have the same options and are populated from a dynamic array. Strangely, there are no error messages in the console to indicate what might be wrong. Any assistance in resolving this issue would be greatly appreciated.

I've searched for similar questions on this platform but haven't found a relevant solution yet. I even attempted to remove the 'overflow: hidden' property, but that didn't solve the problem either.

arr = ["Honda", "Suzuki", "Hyundai"];
var select = document.getElementById("Drop1");
for (var i = 0; i < arr.length; i++) {
  var option = document.createElement("OPTION"),
    txt = document.createTextNode(arr[i]);
  option.appendChild(txt);
  option.setAttribute("value", arr[i]);
  select.insertBefore(option, select.lastChild);
}

var select1 = document.getElementById("Drop2");
for (var i = 0; i < arr.length; i++) {
  var options = document.createElement("OPTIONS"),
    txt = document.createTextNode(arr[i]);
  options.appendChild(txt);
  options.setAttribute("value", arr[i]);
  select1.insertBefore(options, select1.lastChild);
}
<div class="col-3">
  <select id="Drop1" class="mdb-select md-form colorful-select dropdown-dark mx-2" multiple>
    <option value="" disabled selected>Car Features</option>
  </select>
  <label class="mdb-main-label">Car Features</label>
</div>

<div class="col-3">
  <select id="Drop2" class="mdb-select md-form colorful-select dropdown-dark mx-2" multiple>
    <option value="" disabled selected>Bike Features</option>
  </select>
  <label class="mdb-main-label">Bike Features</label>
</div>

Answer №1

You made a mistake in your code:

var options = document.createElement("OPTIONS")
it should be
var options = document.createElement("OPTION")

arr = ["Honda", "Suzuki", "Hyundai"];
var select = document.getElementById("Drop1");
for (var i = 0; i < arr.length; i++) {
  var option = document.createElement("OPTION"),
    txt = document.createTextNode(arr[i]);
  option.appendChild(txt);
  option.setAttribute("value", arr[i]);
  select.insertBefore(option, select.lastChild);
}

var select1 = document.getElementById("Drop2");
for (var i = 0; i < arr.length; i++) {
  var options = document.createElement("OPTION"),
    txt = document.createTextNode(arr[i]);
  options.appendChild(txt);
  options.setAttribute("value", arr[i]);
  select1.insertBefore(options, select1.lastChild);
}
<div class="col-3">
  <select id="Drop1" class="mdb-select md-form colorful-select     dropdown-dark mx-2" multiple>
    <option value="" disabled selected>Car Features</option>
  </select>
  <label class="mdb-main-label">Car Features</label>
</div>

<div class="col-3">
  <select id="Drop2" class="mdb-select md-form colorful-select     dropdown-dark mx-2" multiple>
    <option value="" disabled selected>Bike Features</option>
  </select>
  <label class="mdb-main-label">Bike Features</label>
</div>

Answer №2

<options> is not a valid HTML tag.

It appears there was a typing error. Please fix it by using:

var options = document.createElement("OPTION")

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

Leveraging JS Variables within Twig Template

I am trying to incorporate a JavaScript variable into Twig. Despite attempting to implement the solution mentioned here, my code below does not render the map properly. If I remove the part between "var polylineCoordinates" and "polyline.setMap(map);", th ...

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

VueJs: Finding the corresponding value of a data object

Is there a way to extract data from an object in Vue? This is the format of my data: datasets: [{ text:"Cars", value: "[1,2,3]" }, { text:"Trains", value: "[1,4,10] } ] When I receive information from route props like this: this.selectedText= this ...

Tips for implementing collapsible mobile navigation in Django with the help of Materialize CSS

I'm facing some issues with implementing a responsive navbar that collapses into a 'hamburger bar' on mobile devices and in split view. I have managed to display the hamburger bar, but when I click on it nothing happens. Here's my curre ...

How can you customize gutter widths for specific rows in Bootstrap 3?

Currently, I am constructing my own website using Bootstrap 3 and have come across a challenge that I would like to address: My goal is to assign distinctive gutter widths to certain rows in my design. The remaining sections of the site will adhere to th ...

A div element with a set width that contains an inline child element

I recently created a basic HTML document with two elements: a div with a fixed width, and an image placed after it. According to my understanding, images are inline elements, so I expected the image to appear next to the div on the right side since there ...

retrieve the webpage hosted on the server

Seeking to develop a website where users can input a website URL and have it loaded from the server hosting the HTML page, rather than from the user's computer as iframe does by default. I've experimented with various techniques but all seem to l ...

Tips on how to efficiently wrap content within a column layout, and to seamlessly shrink it if necessary

I recently encountered an issue where I am attempting to create a three-column layout with each column filled with a dynamic number of divs (boxes) ranging from 5 to 15, each with its own height based on its content. These divs are expected to: 1) Be dis ...

Customizing pressed row styles and properties within a map iteration in React Native

How can I modify the style of a single item in a list when a button is pressed in React-Native? I want only the pressed item to change, not the entire row. Here is my attempt at implementing this: //hooks const [activeStyle, setActiveStyle] = useState( ...

How can I launch an application using an .html file on a Linux operating system?

I'm currently involved in an image processing project and have limited knowledge of HTML. Here's how I envision it working. In my C++ application, I have the capability to read an image, process it, and then save the processed image to a file. T ...

How to correctly position a modal in a React application using CSS

Currently, I am working on integrating the react-modal NPM library into a React Typescript project. The issue I am facing is that the modal occupies the entire width of the screen by default, with padding included. My goal is to have it display as a small ...

CORS headers not functioning as expected for Access-Control-Allow-Origin

Can someone help me figure out how to add Access-Control-Allow-Origin: 'http://localhost:8080' in Node.js and Express.js? I keep getting this CORS error: Access to XMLHttpRequest at http://localhost:3000 from origin 'http://localhost:8080&ap ...

Struggling to Load: Ajax Request to Google App Engine Causing Page to

I have developed a page that communicates with a Python application running on Google App Engine to retrieve JSON data using JSONP for cross-origin functionality. However, I am encountering an issue where the page hangs and fails to display the data no mat ...

Swapping out the JSON data from the API with HTML content within the Vue.js application

I am currently working on a project involving Vite+Vue.js where I need to import data from a headless-cms Wordpress using REST API and JSON. The goal is to display the titles and content of the posts, including images when they appear. However, I have enco ...

The navbar toggler icon is not displaying

<header class="container"> <div class="container pt-3 col-md-5 col-sm-5 col-xs-6 text-center"> <a href="#" class="site-logo"><img src="#"></a> <button class="navbar-toggler" type="button" data-toggle="collapse" ...

Enhance the interoperability of Babel with Express.js by steering clear of relative

My current approach to imports is as follows: import router from '../../app/routes' Is there a way to avoid using ../../, for example: import router from 'app/routes'? In typescript, I can achieve this with the following configuratio ...

Implementing a color filter on a camera using Three.js

Can a parameter be adjusted for the camera or renderer to apply a tint to everything on the stage, such as a red shade resembling a "glasses effect", without having to manually modify each object's materials? ...

Navigating Between Pages with Parameters in Ionic 2 (starter app)

I have an Ionic 2 project with a blank template containing a page that displays a list. Upon clicking on an item in the list, the user should be able to view more details about that specific item. Below are the files related to the list: list.html: <i ...

Get notified via email when submitting a form on a PHP contact form

I am a front-end developer and do not have experience with PHP. I am trying to set up a contact form on my website using an example from htmldog. Here is the current code snippet: <form action="contact.php" method="post"> <div class="c ...

Retrieve the value of a CSS class property regardless of whether it is actively being utilized

I am facing a dilemma where I need to determine the value of the 'width' property for a css class named 'foo' (example: ".foo { width:200px}" ). However, there may not be an element with this class in the dom yet. My goal is to retrie ...