Appending select list with items from array

jquery add select option dynamically
jquery append option to select and set selected
jquery add option to select list
how to append values to dropdown using javascript
javascript add options to select dynamically
jquery load select options dynamically
append drop down list in jquery
javascript populate select options from array

I have an array that I would like to use as options in my select dropdown menu. When I run the code, it only appends the first item of that array, and I am not sure why.

 $.ajax({
            type: 'post',
            url: 'Reporting.aspx/actionType',
            contentType: 'application/json; charset=utf-8',
            data: '{ reportType: "' + reportType + '" }',
            dataType: 'json',
            success: function (r, re) {
                $.each(r, function () {
                    testList.push(r.d)
                })

                for (var i = 0; i < testList.length; i++) {

                    $('#drpAction').append('<option>' + testList[0][i] + '</option>')

                }

                console.log(testList)
            },
            failure: function () {
                swal({
                    title: 'Error',
                    text: 'There was an error loading the data',
                    confirmButtonText: 'OK'
                })
            }
        })

I believe you have to loop through the items in testList[0]

var testList = [[1,2,3]]

for (var i = 0; i < testList[0].length; i++) {
  $('#drpAction').append('<option>' + testList[0][i] + '</option>')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="drpAction"></select>

appending options to a select element with an array, So I'm using jquery's .append() to add options to a select box in a dialog box after the page loads and I get the array from the backend. $("#mySelect").append('<​option value="' + value + '">' + tempString + '</option>'). 1. So I'm using jquery's .append() to add options to a select box in a dialog box after the page loads and I get the array from the backend. When I go to appending options to a select element with an array - jQuery Forum

you are looping through testList.length but using testList[0][i].. try using testList[i] instead, like i did here:

for (var i = 0; i < testList.length; i++) {
    $('#drpAction').append('<option>' + testList[i] + '</option>')
}

but, if the items you want to show are inside testList[0], you have too loop through testList[0].length instead of testList.length, like:

for (var i = 0; i < testList[0].length; i++) {
    $('#drpAction').append('<option>' + testList[0][i] + '</option>')
}

if nothing of this is working, i have to see whats your testList looks like.

How to Create a Dropdown List with Array Values using JavaScript , The task is to add elements to the select element from the JavaScript array. each element create an option element and append this new element to the select  The append () method adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list. The syntax of append () method. list.append (item) append () Parameters. The append () method takes a single item and adds it to the end of the list.

Here is your problem testList[0][i], you are looping against testList but you want to access to a list inside testList[0]

you should put the i between the first brackets

$('#drpAction').append('<option>' + testList[i] + '</option>')

or check for testList[0].length

How to add options to a select element using jQuery?, The append() method inserts the specified content as the last child of the jQuery collection. Hence the option is added to the select element. Syntax: $('#selectBox​')  Appending items one at a time on the DOM is considered to be very slow and is highly not advised. Way more performatic is to build a string and append it after the loop – Fabiano Soriani Aug 10 '10 at 4:58

Add a new option to a select with jQuery, Adding a single option can be done by appending HTML to the select box. This method gets a handle to the options and adds a new option to the array of  Adding options to List Box in client side Javascript Options to a drop down list box can be added dynamically using client side JavaScript. We will discuss a simple way of adding options to a list. The process of adding can be controlled based on different condition required. Depending on conditions options can be removed also.

JavaScript to Add and Remove Options in a Select Box, How to add and remove option elements in a select box using DOM methods: There are a variety of approaches for adding and removing option elements in a select An old-fashioned approach uses an option constructor and array syntax. Difference in creation: Unlike list which is a part of Python syntax, an array can only be created by importing the array module. A list can be created by simply putting a sequence of elements around a square bracket. All the above codes are the proofs of this difference. Memory consumption between array and lists: List and array take a

Javascript populate select options from array, Pass an array of objects: [{ value: 'Option 1', label: 'Option 1', selected: true, through your array elements, create a new DOM node for each and append it to  Let’s select elements from it. Select elements from a Numpy array based on Single or Multiple Conditions Let’s apply < operator on above created numpy array i.e. # Comparison Operator will be applied to all elements in array boolArr = arr < 10

Comments
  • what is in testList ?
  • pls show us the array-data. Your example look like its an multidimensional array
  • what does testList.length gives?
  • I've updated the code. it is an ajax post, so the the array testList is different depending on what is selected by the user.
  • @DerekDril, you are most welcome :)