Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:43:10+00:00 2026-06-15T04:43:10+00:00

I am using the innerHTML function to dynamically create a drop-down menu in HTML

  • 0

I am using the innerHTML function to dynamically create a drop-down menu in HTML and populate it with certain parameters. Here is my code:

for (i in categories) {
    var cat_name = i;
    var cats = categories[cat_name];

    P2_txt.innerHTML += cat_name;       

    if (cats.length > 2) {
        // Drop Down Menu Needed
        P2_txt.innerHTML += '<select>';

        for (var j = 0; j < cats.length; j++) {
            P2_txt.innerHTML += '<option>'+cats[j]+'</option>';
        }

        P2_txt.innerHTML += '</select>';
    }   
}

However when I run it, the following HTML code is generated:

<select></select>
<option>Value of cats[0]</option>
<option>Value of cats[1]</option>
<option>Value of cats[2]</option>

Instead of what I want, which is this:

<select>
    <option>Value of cats[0]</option>
    <option>Value of cats[1]</option>
    <option>Value of cats[2]</option>
</select>

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:43:11+00:00Added an answer on June 15, 2026 at 4:43 am

    When you modify innerHTML it’s immediately parsed into the DOM… so you’ve effectively added a select element followed by a bunch of option elements out of the intended hierarchy.

    So, you either:

    1. Build your entire combobox markup and then add it to
      innerHTML
    2. or use the DOM methods createElement and
      appendChild and so forth instead of ugly string concatenation.
    var categories = {
        "Domestic": ["Tabby", "Siamese"],
        "Wild": ["Cougar", "Tiger", "Cheetah"]
      },
      cats,
      combo,
      frag = document.createDocumentFragment();
    
    for (var category in categories) {
    
      cats = categories[category];
    
      frag.appendChild(document.createTextNode(category));
    
      combo = document.createElement("select");
    
      for (var i = 0, ln = cats.length; i < ln; i++) {
        combo.appendChild(document.createElement("option")).textContent = cats[i];
      }
    
      frag.appendChild(combo);
    }
    
    document.body.appendChild(frag);

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i created four drop down select lists in html and dynamically load them from
I am using following code to create a list dynamically. It works fine but
I have the following bit of code. I'm trying to dynamically add drop-down options
I'm dynamically inserting some html into the document (by using obj.innerHTML += 'some html').
I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.
I'm using JavaScript to dynamically add rows to a table, I create some textboxes
I dynamically add rows to divStaff using jquery: $(span[id$='lblAddStaff']).click(function() { //$(.staff_tpl).find(input[id$='txtRate']).val(0,00); var staff_row =
i'm using this to generate a textbox dynamically:` <html> <head> <title>Dynamic Form</title> <script type=text/javascript
In the code HTML+Script below, an event handler is attached after page load using
I'm using this JS function to dynamically add labels to a table, this function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.