Reputation: 27
I have two javascript functions madeAjaxCall() and getBookCall(bookId) to obtain book list and chapter list respectively.
I'm trying to call getBookCall(bookId) from within the function madeAjaxCall().
function madeAjaxCall(){
$.ajax({
type: "GET",
url: "http://localhost:8080/restApp/book/list",
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
delete_table();
if(data){
var len = data.length;
var txt = "";
txt += "<tr><th>"+"bookId"+"</th><th>"+"bookName"+"</th><th>"+"Chapter Details"+"</th></tr>";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].bookId != null && data[i].bookName != null){
/* txt += "<tr><td>"+data[i].bookId+"</td><td>"+data[i].bookName+"</td><td><a href="+ "http://localhost:8080/restApp/chapter/list/"+ data[i].bookId +">"+"Chapter details"+"</a></td></tr>"; */
txt += "<tr><td>"+data[i].bookId+"</td><td>"+data[i].bookName+"</td><td><a href="+ "#"+" "+ "onclick=" + +"getBookCall("+ data[i].bookId + ")"+";return false;" +">"+"Chapter details"+"</a></td></tr>";
}
}
if(txt != ""){
$("#table1").append(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
return false;
}
And the other function
function getBookCall(bookId){
$.ajax({
type: "GET",
url: "http://localhost:8080/restApp/chapter/list/"+bookId,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
delete_table2();
if(data){
var len = data.length;
var txt = "";
txt += "<tr><th>"+"chapterId"+"</th><th>"+"chapterName"+"</th></tr>";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].chapterId != null && data[i].chapterName != null){
txt += "<tr><td>"+data[i].chapterId+"</td><td>"+data[i].chapterName+"</td></tr>";
}
}
if(txt != ""){
$("#table2").append(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
return false;
}
I am obtaining list of books as JSON from the function madeAjaxCall() and appending the list in a table. I want to call the function getBookCall(bookId) from within the function madeAjaxCall() with the help of a href. But i am unable to call the function getBookCall(bookId) using a href, from within the function madeAjaxCall().
This is the line from the function madeAjaxCall from where the function getBookCall(bookId) could not be called.
txt += "<tr><td>"+data[i].bookId+"</td><td>"+data[i].bookName+"</td><td><a href="+ "#"+" "+ "onclick=" + +"getBookCall("+ data[i].bookId + ")"+";return false;" +">"+"Chapter details"+"</a></td></tr>";
Upvotes: 0
Views: 122
Reputation: 133403
Here In example I have use custom data-*
attribute to store bookid
, which can be fetched using .data()
, create your anchor like:
txt += '<a class="myBookLink" href="#" data-bookid="' + data[i].bookId + '">Chapter details</a>";
Then use Event Delegation using .on() delegated-events approach to bind the click event handler of anchor.
$(document).on('click', function(){
getBookCall($(this).data('bookid'))
return false;
})
Important: In place of document
you should always use closest static container.
For Immediate solution use quotes properly.
txt += "<tr><td>"
+ data[i].bookId
+ "</td><td>"
+ data[i].bookName
+'</td><td><a href="#" onclick="getBookCall('+ data[i].bookId + ');return false;">Chapter details</a></td></tr>';
instead of
txt += "<tr><td>"+data[i].bookId+"</td><td>"+data[i].bookName+"</td><td><a href="+ "#"+" "+ "onclick=" + +"getBookCall("+ data[i].bookId + ")"+";return false;" +">"+"Chapter details"+"</a></td></tr>";
Upvotes: 3