0

I have an array var myList = new Array(); and I have a drop down

@Html.DropDownList("yourList", Model.yourList). 

How can I fill the dropdownlist using javascript.

0

2 Answers 2

1
var myList = new Array();

for (i=0; i<myList.length; i++) {
    master.options[master.options.length]=new Option(myList[i].text, myList[i].value);
}

http://www.javascriptkit.com/javatutors/selectcontent.shtml

Sign up to request clarification or add additional context in comments.

1 Comment

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
1
var myList = new Array();
myList.push({ value: '1', text: 'item 1' });
myList.push({ value: '2', text: 'item 2' });
myList.push({ value: '3', text: 'item 3' });
$('#yourList option').remove();
$.each(myList, function () {
    $('<option/>', {
        value: this.value,
        html: this.text
    }).appendTo('#yourList');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.