Reputation: 2036
can you guys help me debug why my jquery click event is not working.
$(function(){
displayBooks();
$(".btn_checkout").click(function(){
//checkOutBook(event.target.id);
alert('got in here');
});
});
function checkOutBook(id)
{
alert('Checkout book id ' + id);
}
function displayBooks(){
var url = 'http://portal.internal.urs.org/tools_services/training_library/_vti_bin/listdata.svc/Book?$expand=CheckedOutTo';
var books = new Array();
$.getJSON(url, function(data){
for (var i = 0; i < data.d.results.length; i++){
var book = data.d.results[i];
books[i] = book;
var s = '<tr><td>' + book.Title + '</td>';
if (book.CheckedOutTo != null){
s += '<td>' + book.CheckedOutTo.Name + '</td>';
} else {
s += '<td> <button class="btn_checkout" type="button" id="'+book.Id+'">Check Out</button></td>'
}
s += '</tr>';
$('.LibraryTable').append(s);
}
});
return books;
};
The alert 'got in here'
does not alert.
Thanks.
Upvotes: 0
Views: 40
Reputation: 178413
You need to attach the event at the level of the parent
$('.LibraryTable').on("click",".btn_checkout",function ()...
Upvotes: 2
Reputation: 15837
function displayBooks(){
var url =
'http://portal.internal.urs.org/tools_services/training_library/_vti_bin/listdata.svc/Book?$expand=CheckedOutTo';
var books = new Array();
$.getJSON(url, function(data){
for (var i = 0; i < data.d.results.length; i++){
var book = data.d.results[i];
books[i] = book;
var s = '<tr><td>' + book.Title + '</td>';
if (book.CheckedOutTo != null){
s += '<td>' + book.CheckedOutTo.Name + '</td>';
} else {
s += '<td> <button class="btn_checkout" type="button" id="'+book.Id+'">Check Out</button></td>'
}
s += '</tr>';
$('.LibraryTable').append(s);
}
}); //remove this
Upvotes: 1