Skip to content Skip to sidebar Skip to footer

Keeping Selected An Option In Select Dropdown With Jquery After Refresh

I have small script that reloads a select containing the nicknames of the users in a chatroom. When someone clicks the select, the javascript refreshes it, repopulating the option

Solution 1:

AJAX is asynchronous so you are trying to reset the value before the data will be populated. Once the options are replaced the select will lose the value again

Use the complete callback of load()

$("#Users").focusin(function() {
    var$select=$(this),  UsersSelect = $select.val();
    $select.load("users.php", function(){
       /* new html exists now s we can set value*/$select.val(UsersSelect);
    });
})

Post a Comment for "Keeping Selected An Option In Select Dropdown With Jquery After Refresh"