Jquery clear select in the second populate

I have this jquery and it is working fine. but the problem that i am facing when i select second time it will add the data to the previous results. and will not clear it. I tried to use empty function and .html('') but it did not work.

$(document).ready(function(){  $("#cat").change(function(){
$.ajax(
{url:"maintenance_task.php",
data:   { cat_id : $("#cat option:selected").val() },
dataType: 'json',
success:function(json){
     $.each(json, function(i, value) {

        $('#task_type').append($('<option>').text(value).attr('value', i));
    });
  }});  });});

can anybody help me with this. thanks in advance.

Answers


try this

$(document).ready(function(){  
 $("#cat").change(function(){
   $.ajax({
      url:"maintenance_task.php",
      data:   { cat_id : $("#cat option:selected").val() },
      dataType: 'json',
      success:function(json){
        $('#task_type').html(' ');
        $.each(json, function(i, value) {
          $('#task_type').append($('<option>').text(value).attr('value', i));
        });
     }
  }); 
 });
});

Need Your Help

jquery image slideshow with links per image

javascript jquery html website

i'm in search of a simple jquery image flipper/fader, that will cycle through a bunch of images, each of them with it's own link.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.