title: Keeping selected an option in select dropdown with jquery after refresh
body: <p>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 from a php page to add people that have logged in and removing those who logged out.
  The script works properly on reloading the select, but I want it to avoid losing the precedent selected option if a user clicks the select another time to refresh the list, and the script doesn't keep the correct option. All the code works properly, except that part of the jquery.</p>
  
  <p>The JS (the jquery library is of course also on the page):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(document).ready(function() {
      $("#Users").focusin(function() {
        var UsersSelect = $('#Users').val();
        $("#Users").load("users.php");
        $('select[name="Users"]').val(UsersSelect);
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>The html/php webpage:</p>
  
  <pre><code>&lt;div id="UserList" name="UserList"&gt;
  &lt;select name="Users" id="Users"&gt;
  &lt;?
  $queryprep = "select name FROM users";
  $query = $PDO-&gt;prepare($queryprep);
  $query-&gt;execute();
  while($rs = $query-&gt;fetch()) {
      echo "&lt;option value=".$rs['name']."&gt;".$rs['name']."&lt;/option&gt;";
  }
  $query = null;
  ?&gt;
  &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The users.php page:</p>
  
  <pre><code>&lt;select name="Users" id="Users"&gt;
  &lt;?
  $queryprep = "select name FROM users";
  $query = $PDO-&gt;prepare($queryprep);
  $query-&gt;execute();
  while($rs = $query-&gt;fetch()) {
      echo "&lt;option value=".$rs['name']."&gt;".$rs['name']."&lt;/option&gt;";
  }
  $query = null;
  ?&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Thanks for the answers.</p>