-2

I want to create an click event for a dynamically generated button. How can I do that?

I thought that this code will work, but it doesn't. How can I add the function?

  $(document).ready(function() {
    $("#keyword_1").click(function() {
      var keyword_js = document.getElementById("keyword_1").value;
      $.ajax({
          url: "list",
          type: "POST",
          async: false,
          data: {
            keyword: keyword_js.toString()
          },
          dataType: "json",
          success: function(data) {
            //button
          }
        }
      },
      error: function(request, status, error) {
        alert("code:" + request.status + "\n" + "message:" + request.responseText + "\n" + "error:" + error);
      },
      complete: function() {
        // call back method
      }
    });
  });
});

function insRow() {
  var keyword_js = document.getElementById("keyword_en").value;
  oTbl = document.getElementById("addTable");
  var oRow = oTbl.insertRow();
  oRow.onmouseover = function() {
    oTbl.clickedRowIndex = this.rowIndex
  }; //clickedRowIndex
  var oCell = oRow.insertCell();

  //Form Tag
  var frmTag = "<input type=button value=" + keyword_js + " id='keyword_1'>";
  frmTag += "<input type=button value=‘del’ onClick='removeRow()' style='cursor:hand;'>";
  oCell.innerHTML = frmTag;
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Tom. J
  • 1

1 Answers1

0

Maybe you can do like this.

$(document).on("click","#keyword_1",function(){ 

//do something.
 });
HyungjinJeon
  • 143
  • 11