title: Add optgroup with options to select with jQuery
body: <p>my question is simple but yet I didn't find a clear example of doing that.
  I'm trying to add optgroup for a select and fill it with options using jquery for some reason it's not working.
  this is my code:</p>
  
  <pre><code>$('#ptype').change(function() {
          $.post(
                     "/sell/site/index/categories", 
                     {
                        'parent_id': $('#ptype').val()
                     }, 
                     function(response){
                         var categories = $.parseJSON(response);
  
                         $('#sub_category').empty();
                          $.each(categories, function (index) {
  
                              var optgroup = $('&lt;optgroup/&gt;');
  
                              $.each(this.children, function (index) {
                                  optgroup.add($("&lt;option&gt;&lt;/option&gt;")
                                              .attr("value",index)
                                              .text(this)); 
                              });
  
                              $("#sub_category").append(optgroup);
  
                          });
  
                          $("#sub_category").multiselect('refresh'); //refresh the select here
                      }
                  );
          });
  </code></pre>
  
  <p>Any help would be greatly appreciated!
  Thanks! </p>