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

  • Home
  • SEARCH
  • 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 9028163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:55:10+00:00 2026-06-16T06:55:10+00:00

I have a simple question. I have a select list like this: var myarray

  • 0

I have a simple question. I have a select list like this:

var myarray = ["one", "two", "three"];
var container = document.createElement("select");  
for (var i = 0; i < myarray.length; i++) {
    var element = document.createElement("option");
    var textlabel = document.createTextNode(myarray[i]);

    if (element.nodeValue == "two") { 
        element.selected = true;
    }

    element.appendChild(textlabel); 
    container.appendChild(element);
}
document.body.appendChild(container);

I have two questions about it:

1) I am pretty sure that the element that should be selected right now is “two”… isn’t it?

2) Since the option elements are being created dinamically inside a loop (there are no three different option variables for me to play with, but just one that gets renewed as the loop goes forward), how do I reference the selected one for future uses?

For example, imagine that later on I get user input, and according to that input I want that this list has as a selected item, “three”.

Thank you for any help! Here is the fiddle if you want to use it…

  • 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-16T06:55:11+00:00Added an answer on June 16, 2026 at 6:55 am

    1) I am pretty sure that the element that should be selected right now
    is “two”… isn’t it?

    No, it’s not: you check element.nodeValue, while in fact you should’ve been checking textLabel‘s one – or just the content itself:

    if (myarray[i] === 'two') {
      element.selected = true;
    }
    

    2) Since the option elements are being created dinamically inside a
    loop (there are no three different option variables for me to play
    with, but just one that gets renewed as the loop goes forward), how do
    I reference the selected one for future uses?

    See, <select> elements has two useful properties: options (which contains all the options in it, and is updated dynamically) and selectedIndex. You can combine them to get the selected option:

    container.addEventListener('change', function() {
       console.log(this.options[this.selectedIndex]);
    }, false);
    

    But if what you want is to know the value of selected element, that’s even easier – with container.value.

    For example, imagine that later on I get user input, and according to
    that input I want that this list has as a selected item, “three”.

    That’s piece of cake if you know the position of the option that corresponds to this: just use selectedIndex property again:

    container.selectedIndex = 3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hopefully a pretty simple question this time. I have a Select method in a
i have a simple question: i have this var: $v = 24000,1500,1500,1500,1500,1500,; i would
So i have a List of strings that looks like this: var ls=new List<string>()
This seems like a fundamentally simple question. I have a WinForms dialog box with
I have a simple question about the expression in jquery to select a class
This is a real newbie question. I have simple app that selects a picture
I have a simple question. I'm trying to get a list of the indicators
I have MySQL database with two tables: users and items. They look like this:
This seems like a very simple question, but I'm getting lost, and need a
Background: I have two web pages, one is a list of things, and the

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.