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();
      $("#myDiv").append($("#myDrop"));
      $("#myDrop").show();
       for (var i = 0; i < array.length; i++) {
         var id = "#" + array[i].id;
         var newOption = "<option value ='" + i + "'>" + $(id).text() + "</option>";
         $("#myDrop").append(newOption);
      }

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

Answers


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.

https://jsfiddle.net/w5vkeswr/

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

Post Status

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

Search




Leave an answer