Home Ask Login Register

Developers Planet

Your answer is one click away!

vedat February 2016

add item to dropdownlist when dropdownlist's display is none

I have a aspx dropdown list and its' display is none. I'm adding items from array to dropdownlist but it's not working. Here is my script code.

  var array = $("#listed h1").toArray();
       for (var i = 0; i < array.length; i++) {
         var id = "#" + array[i].id;
         var newOption = "<option value ='" + i + "'>" + $(id).text() + "</option>";

Can anyone help me what I'm doing false? Thanks.


Dustin Poissant February 2016

You provided no HTML for us to work with so I will have to guess what you HTML probably looks like from your JS.

It seems like you might have some <h1>s inside a <div> and a <select> that is probably blank and hidden by default.

Then on page ready you want to populate the select with options that represent the <h1>s with the value being an index that represents the order they are displayed in, and the text being the text of the <h1>.

This of course is all a bunch of assumptions I have made based on the limited information you provided.

Alright here is my attempt to understand the question. I will update the answer as you provide more information.


$("div h1").each(function(i, el){
	$("select").append("<option value='"+i+"'>"+el.innerHTML+"</option>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <h1>Item 1</h1>
  <h1>Item 2</h1>
  <h1>Item 3</h1>
<select style='display: none;'></select>

Post Status

Asked in February 2016
Viewed 2,801 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life