0
function loadProvinces(data){

var province = "<tr><th>Options</th><th>Province</th></tr>";
var json = $.parseJSON(data);

for(var i=0; i<json.province_info.length; i++){
    province += "<tr><td><input type='button' id='"+json.province_info[i].province_id+"' value='Delete' /></td><td>"+json.province_info[i].province_name+"</td></tr>";
 }

  $("#appendprovince").empty();
  $("#appendprovince").append(province);
  $("#appendprovince").show();  

  $(":input").click(function(e) {

    var id = this.id;
    var url = "http://localhost:8080/CurdServlet/ProvinceServlet";
    $.post(url,{"getProvince" : "delete_province","province_id":""+id},
        function(data){
            alert(data);     // its working and shows data successfully 
        loadProvinces(data); // here loadProvinces function not working only
   }); 

});

}//end loadProvinces

i have called loadprovinces function when user add a province for a particular country as user adds a province it inserts into a province table so it is working , a problem is that loadProvinces function is not calling when user presses a delete button .

trainoasis
  • 6,419
  • 12
  • 51
  • 82

3 Answers3

0

Why don't you just do it this way:

function loadProvinces (data) {

    var province = "<tr><th>Options</th><th>Province</th></tr>";
    var json = $.parseJSON(data);

    for(var i = 0; i < json.province_info.length; i++){
        province += "<tr><td><input type='button' id='" + json.province_info[i].province_id + "' value='Delete' /></td><td>" + json.province_info[i].province_name + "</td></tr>";
    }

    $("#appendprovince").empty();
    $("#appendprovince").append(province);
    $("#appendprovince").show();
}
;//end loadProvinces

$(":input").click(function (e) { // it would be nice to add some more identifiers e.g. class to fire only when needed

    var id = this.id;
    var url = "http://localhost:8080/CurdServlet/ProvinceServlet";
    $.post(url, {"getProvince": "delete_province", "province_id": "" + id},
    function (data) {
        alert(data);     // its working and shows data successfully
        loadProvinces(data); // here loadProvinces function not working only
    });
});
MythThrazz
  • 1,629
  • 17
  • 25
  • Fixed the syntax. Still, think of adding some more identifiers to the line firing the 'click' event. Because now it will fire each time you click *ANY* input. – MythThrazz Jul 30 '14 at 07:57
0

There is syntax error. Not sure if it is the main Problem, but it can not works without this.

Have you checked console? F12

function(data){ miss }

m1k1o
  • 2,344
  • 16
  • 27
0

As you are adding delete button dynamically then you shuld use .on to bind click event. Also keep this bind click event code outside function :

  function loadProvinces(data){

     var province = "<tr><th>Options</th><th>Province</th></tr>";
     var json = $.parseJSON(data);

     for(var i=0; i<json.province_info.length; i++){
         province += "<tr><td><input type='button' id='"
                  +json.province_info[i].province_id
                  +"' value='Delete' /></td><td>"
                  +json.province_info[i].province_name+"</td></tr>";
     }

     $("#appendprovince").empty();
     $("#appendprovince").append(province);
     $("#appendprovince").show();  

}//end loadProvinces

// bind click event for dynamically generated element
$(document).on("click",":input",function(e) {

   var id = this.id;
   var url = "http://localhost:8080/CurdServlet/ProvinceServlet";
   $.post(url,{"getProvince" : "delete_province","province_id":""+id},
        function(data){
           alert(data);     // its working and shows data successfully 
           loadProvinces(data); // here loadProvinces function not working only
    }); 
 }); 
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57