After appending table I am checking on button click event. Javascript alert is working but jQuery click event is not working.
I cannot seem to find my mistake.
$("#inputcon").change(function(e){
if($(this).attr('checked')){
$(this).val('TRUE');
}else{
$(this).val('FALSE');
}
if($(this).val() == 'TRUE'){
$("div#Add").append(' <table id="inputT" style=""> <tr> ' +
'<td><label>Input Control Name :</label> </td>' +
' <td><input id="icname" type="text" name="icname" ></td>' +
' </tr>' +
' <tr>' +
' <td><label>Parameter Name :</label> </td>' +
' <td><input id="pname" type="text" name="pname" ></td>' +
' </tr> ' +
' <tr >' +
' <td><label>DataType:</label> </td>' +
' <td><select name="datatype" class="texta" id="datatype" style="width: 328px " > ' +
' <option value="string">Text</option> ' +
' <option value="date">Date</option>' +
' <option value="number">Number</option>' +
' </select></td>' +
' </tr>' +
' <tr >' +
' <td><label>Input Type:</label> </td>' +
' <td> <select name="inputtype" class="texta" id="inputtype" style="width: 328px " >' +
' <option value="text">Text</option>' +
' <option value="date">Date</option>' +
' <option value="singleList">Single Select List</option>' +
' <option value="singleQue" >Single Select Query List</option>' +
' <option value="multiList" selected="true">Multi Select List</option>' +
' <option value="multiQue" selected="true">Multi Select Query List</option>' +
' </select></td>' +
' </tr><tr><td><input onClick="alert(this.id);" id="inconbutt" type="button" value="Add InputControl" name="Add InputControl"></td></tr> </table>');
}else{
$("table#inputT").remove();
}
});
$("#inconbutt").click(function() {
alert("add");
var i1 = $("#icname").val() ;
var pname = $("#pname").val();
var dtype = $("#datatype").val();
var itype = $("#inputtype").val() ;
alert(i1);
alert(pname);
alert(dtype);
alert(itype);
/* $.ajax({
type:"POST",
url:"Welcome.jsp",
data:"inputname="+i1+"&pname="+pname+"&datatype="+dtype+"&intype"+itype,
success: function(){
$("table#inputT").remove();
$("table#tableI").append(' <tr><td><a href="#">'+i1+'</a></td><td><a href="#">Remove</a></td></tr> ' ) ;
}
}); */
});
The jQuery function you created with .click at bind at the start of the load and your input is being placed after that. So jQuery doesn’t recognize the event and your function isn’t called.
Try using dynamic binding functions such as:
OR
OR if you are using newer version of jQuery 1.7+ then you can also use