Paul Smith Paul Smith - 6 months ago 23
jQuery Question

populate dropdown using jquery

I am trying to populate a drop down using JQuery but failing as I normally do, I can get the data to display in a Div but not in a drop down

I have a select id of name-data here is the JQuery code, what am I doing worng guys?

$('input#namein').on('change', function(){

var name = $('input#namein').val();
if ($.trim(name) != '') {
$.post('ajax/name.php', {name: name}, success = function(data) {

var options = "";

for(var i = 0; i < data.length; i++)
{
options += "<option value='" + data[i].toLowerCase() + "'>" + data[i] + "</option>";
}

$("div#name-data").append(options);

});


}

});


HTML:

<html>
<head></head>
<body>
Service user time Slots:
<input type="text" id="namein" value="Name">
Time Slots:
<form action="times" method="">
<select id="name-data"></select>
</form>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/golabl1.js"></script>
</body>
</html>

Answer

Cheers Guys go it, I used

$( document ).ready(function() {

console.log( "ready!" );

        $.post('ajax/name1.php', success = function(data) {
                if (data != ''){
$('#namein').append(data).show();

}

Comments