> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/select-box-change-dependent-options-dynamically-javascript-object.md).

# Select Box Change Dependent Options dynamically (JavaScript Object)

<figure><img src="/files/mbJFpQ5vKlJeu8n6cZhY" alt=""><figcaption></figcaption></figure>

### Example 1

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">
  <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <link rel="stylesheet" href="jquery.sweet-modal.min.css">
  <title>Document</title>
</head>

<body>
  <div class="wrapper">
    <select id="parent_select">
      <option>----</option>
    </select>
    <select id="child_select"></select>
  </div>
  <script language="javascript" type="text/javascript">
  var mList = {
    Chocolates: [
      'Bournville',
      'Snickers',
      'Twix',
      'Ferrero Rocher',
      'Dark Chocolate',
      'Milk chocolate',
      'White chocolate',
      'Gianduja chocolate'
    ],
    Vegetables: [
      'Broccoli',
      'Cabbage',
      'Carrot',
      'Cauliflower'
    ],
    Icecreams: [
      'Black Raspberry',
      'Frozen pudding',
      'Neapolitan',
      'Strawberry',
      'Vanilla',
      'Chokecherry',
      'Frozen yogurt',
      'Booza',
      'Frozen yogurt',
      'Ice milk'
    ],
    Mobiles: [
      'Apple iPhone',
      'Samsung Galaxy',
      'Realme',
      'Huawei',
      'OnePlus',
      'Asus ROG',
      'Xiaomi',
      'Vivo'
    ]
  };
  el_parent = document.getElementById("parent_select");
  el_child = document.getElementById("child_select");
  for (key in mList) {
    el_parent.innerHTML = el_parent.innerHTML + '<option>' + key + '</option>';
  }
  el_parent.addEventListener('change', function populate_child(e) {
    el_child.innerHTML = '';
    itm = e.target.value;
    if (itm in mList) {
      for (i = 0; i < mList[itm].length; i++) {
        el_child.innerHTML = el_child.innerHTML + '<option>' + mList[itm][i] + '</option>';
      }
    }
  });
  </script>
</body>

</html>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/select-box-change-dependent-options-dynamically-javascript-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
