0

We are using jQuery ajax in our application.

For example to get the states in a country ,after getting the result from server then we are doing the following thing.

$.each(results , function(index,value){

  $('#id').append('<option value="'+index+'">'+value+'</option>');

});//Here `id` the select box id

It is working perfectly. But I need a better readable solution. Is there any other way to do this?

Nick N.
  • 12,902
  • 7
  • 57
  • 75
PSR
  • 39,804
  • 41
  • 111
  • 151

1 Answers1

1

Just a simple optimization should be enough:

(function () {
    var str = "";
    $.each(results, function (index, value) {
        str += '<option value="' + index + '">' + value + '</option>';
    });
    document.getElementById('id').innerHTML += html;
})();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155