0

Im trying to populate a dropdown list using jquery. The population has but the default text "Select Model" is replaced by the new data..

Javascript:

 data = $.map(data, function (item, a) {
    return"<option value=" + item.Value + ">" + item.Text    + "</option>";
    $("#SelectedModel").html(data.join(""));

HTML:

@Html.DropDownListFor(m=>m.SelectedModel,Model.models,"Select Model")

1 Answer 1

1

Use append:

//to reset select options
$('#SelectedModel option').not(':eq(0)').remove();
//then append data    
$("#SelectedModel").append(data.join(""));

Or with .html(), should be:

$("#SelectedModel").html($("#SelectedModel").html()+data.join(""));
Sign up to request clarification or add additional context in comments.

2 Comments

This appends the data from previous ajax call. I want to replace all the options but keep the dropdown label"Select Model"
Dude you rock socks. Thanks a lot buddy it works. I spent hours trying to make this work.

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.