Joey Morani Joey Morani - 9 months ago 45
Javascript Question

How to set options of a dropdown using Javascript/jQuery?

Does anyone know how to set the options and the values of a dropdown menu using javascript or jquery? I'm using this HTML:

<select size="1" id="D1">

Thanks for the help.

Answer Source

You don't even necessarily need jQuery:

var select = document.getElementById("D1"),
    opt = document.createElement("option");
opt.value = "value";
opt.textContent = "text to be displayed";


But here it is with jQuery anyway:

$("select#D1").append( $("<option>")
    .html("text to be displayed")