Home > Web Front-end > JS Tutorial > body text

How to Populate One Dropdown Based on Selection in Another Using JavaScript?

Linda Hamilton
Release: 2024-10-18 13:08:03
Original
611 people have browsed it

How to Populate One Dropdown Based on Selection in Another Using JavaScript?

Populate one dropdown based on selection in another using JavaScript

When faced with the task of dynamically updating the options in one dropdown based on the selection made in another, it's essential to avoid unnecessary complexities like database queries. Let's explore a straightforward JavaScript-based solution that accomplishes this without the need for AJAX calls.

To illustrate this approach, consider a scenario where you have two dropdown lists: Dropdown A with options such as "Colors," "Shapes," and "Names" and Dropdown B. We want to populate the options in Dropdown B based on the selection made in Dropdown A.

Sample Code

The following JavaScript function demonstrates how to achieve this functionality:

<code class="javascript">function configureDropDownLists(ddl1, ddl2) {
  var colours = ['Black', 'White', 'Blue'];
  var shapes = ['Square', 'Circle', 'Triangle'];
  var names = ['John', 'David', 'Sarah'];

  switch (ddl1.value) {
    case 'Colours':
      ddl2.options.length = 0;
      for (i = 0; i < colours.length; i++) {
        createOption(ddl2, colours[i], colours[i]);
      }
      break;
    case 'Shapes':
      ddl2.options.length = 0;
      for (i = 0; i < shapes.length; i++) {
        createOption(ddl2, shapes[i], shapes[i]);
      }
      break;
    case 'Names':
      ddl2.options.length = 0;
      for (i = 0; i < names.length; i++) {
        createOption(ddl2, names[i], names[i]);
      }
      break;
    default:
      ddl2.options.length = 0;
      break;
  }
}

function createOption(ddl, text, value) {
  var opt = document.createElement('option');
  opt.value = value;
  opt.text = text;
  ddl.options.add(opt);
}
Copy after login

HTML Markup

The following HTML markup includes two dropdown lists:

<code class="html"><select id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))">
  <option value=""></option>
  <option value="Colours">Colours</option>
  <option value="Shapes">Shapes</option>
  <option value="Names">Names</option>
</select>

<select id="ddl2"></select></code>
Copy after login

Usage

To use this functionality, simply call the configureDropDownLists function, passing the first dropdown (ddl1) and the second dropdown (ddl2) as arguments. The function will handle the dynamic population of Dropdown B based on the selection made in Dropdown A.

This code snippet demonstrates a complete working solution, allowing you to dynamically populate one dropdown based on the selection made in another using JavaScript, without the need for database calls or AJAX requests.

The above is the detailed content of How to Populate One Dropdown Based on Selection in Another Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template