title: Set SELECT value after filling it with jQuery
body: <p>I'm trying to populate a SELECT using jQuery and after it's populated set the value i want.
  I'm working with ASP.NET MVC 5.</p>
  
  <p><strong>The problem is the value doesn't get set</strong></p>
  
  <p>Here's my code:</p>
  
  <pre><code>$(document).ready(function () {
      //DropDownLists Initialization
      ListCategories(); //Populates the dropdownlist
      PreviousCategory(); //Sets DropDownList value to previous state (posted value)
  });
  function PreviousCategory() {
      var previousCategory = $("#PreviousCategory").val();
      if (previousCategory != null &amp;&amp; previousCategory != '') {
          $("#IdCategory").val(previousCategory);
      }
  }
  </code></pre>
  
  <p>$("#PreviousCategory") is a hidden input wich gets it's value server-side after a postback with the next code:</p>
  
  <pre><code>@if (ViewBag.Category!=null)
  {
      @Html.Hidden("PreviousCategory",(object)ViewBag.Category);
  }
  </code></pre>
  
  <p>Both functions work separately, the DropDownList gets populated flawlessly, but the value doesn't get set.
  <strong>If i trigger PreviousCategory() from another event (for example a button click), the value gets set perfectly.</strong></p>
  
  <p>I didn't think it was necessary to post ListCategories() code since it works well and you can just assume it fills the dropdownlist, though if anyone find it necessary let me know and i'll edit the post. </p>
  
  <p>EDIT:</p>
  
  <p>Here is ListCategories() code:</p>
  
  <pre><code>function ListCategories(){
      _idOrganigrama = $("#IdOrganigrama").val()
      _idTipoPedido = $("#IdTipoPedido").val()
      data = { idOrganigrama: _idOrganigrama, idTipoPedido: _idTipoPedido }
      $.post("ListCategories/", data, function (categoryList) {
          $("#IdCategoria").empty();
          $(categoryList).each(function () {
              $("&lt;option /&gt;", {
                  val: this.Id,
                  text: this.Descripcion
              }).appendTo($("#IdCategory"));
          });
      });
  }
  </code></pre>
  
  <p>By the way...$("#IdCategory") is the select.</p>