I cannot change the value of a dynamically populated select using JQuery

222
March 18, 2017, at 07:05 AM

I dynamically populate a select with the data coming from a DB.

$.each(response[0], function(key, value) {
    $('#localita').append($("<option></option>")
    .attr("value",key)
    .text(value));
});

Everything is ok except for the fact that I cannot change the selected option based on the value of another field (loc2).

$('#localita').val($('#loc2').val());

If I write:

alert($('#localita').val())

I obtain the right value but on screen there is always the original value. What is wrong with my code?

Thank you for your help.

READ ALSO
How would you show a live graph of button presses vs. time?

How would you show a live graph of button presses vs. time?

I've got an idea for a simple websiteIt would't be much more than a button

233
JQuery UI draggable along with pointer

JQuery UI draggable along with pointer

Am building a page which has draggable panelIn which it has a standard grid

337
Creating second popover within first popover

Creating second popover within first popover

Trying to select the current user when click on the coffee button inside popoverUsers are populated in the home page

261