Raviteja Raviteja - 9 days ago 4
jQuery Question

How to get selected row in html table which is binded by sql using jquery and webservice?

Am trying to get the value which is binded in html table using Jquery and webservice but the event is not firing where the values bind by jquery but the event is firing when i am binding from html but i want when the table is binded by jquery only

please help me ...

My code is like this

Jquery to print selected row's id

$(document).ready(function () {
$('#tbDetails tr').click(function (event) {
alert($(this).attr('Eid'));
});
});


Jquery binded from webservice

$(document).ready(function () {
$.ajax({
type: "POST",
url: "../../Services/Employee/BindEmployees.asmx/BindDatatable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {

for (var i = 0; i < data.d.length; i++) {
$("#tbDetails").append("<tr><td>" + data.d[i].Eid + "</td><td>" + data.d[i].Ename + "</td><td>" + data.d[i].Edesg + "</td><td>" + data.d[i].Edept + "</td><td>" + data.d[i].Edoj + "</td><td>" + data.d[i].Etype + "</td><td>" + data.d[i].Epfno + "</td><td>" + data.d[i].Ebankac + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
});


Binding is working fine but the only problem is when am trying to get the value from select tablerow it is not firing ....

Answer

Your problem is that you attach the click event to your tr elements when the page is loaded, therefore the click event handler will not be attached to tr elements created later than the page is loaded. Use .on() instead:

$(document).ready(function () {
$('#tbDetails').on("click", "tr", function (event) {
    alert($(this).attr('Eid'));       
});
});

This will apply for future tr items as well inside #tbDetails.

Comments