-3
$.each(obj.query, function(k,v) {
  var tbl_row = "";

  $.each(this, function(k , v) {
    tbl_row += "<td>"+v+"</td>";
  })
  tbl_row += "<td><a href='#' onclick='"+javascript:makeajaxcall(v.uID);+"'>Delete</a></td>";
  tbl_body += "<tr>"+tbl_row+"</tr>";                
})
$("#201").html(tbl_body);

I need to add a link to a tab and call a javascript function and if I just add the <a> tag it will work but when I add href="" and onclick="" it will not work. This is an AJAX call data printed in dynamic table.

user2422387
  • 17
  • 1
  • 3
  • 7

3 Answers3

0

Best way to do this is using jquery's "on" event listener which allows bind event listener to dynamic added html elements. Here is what I suggest:

First change:

tbl_row += "<td><a href="#" onclick='"+javascript:makeajaxcall(v.uID);+"'>Delete</a></td>";

to:

tbl_row += '<td><a href="#" id="makeajaxCallTirggerId" data-ajaxParam=' + v.uID + '>Delete</a></td>';

Then in your java script do the following;

$(document).on('click', '#makeajaxCallTirggerId', function(e){
   makeajaxcall($(this).attr('data-ajaxParam'));
});
zhumingvictor
  • 109
  • 2
  • 6
0

At the risk of doing the work for you...

change

tbl_row += "<td><a href="#" onclick='"+javascript:makeajaxcall(v.uID);+"'>Delete</a></td>";

to

tbl_row += "<td><a href='#' onclick='"+javascript:makeajaxcall(v.uID);+"'>Delete</a></td>";

...notice the difference in the syntax highlighting, that gives the answer away...

The problem is that you're trying to add href="" and onclick="" to a string surrounded by " ". You need to be adding href='' and onclick='' instead. see this post for a similar situation Are single quotes allowed in HTML?

EDIT If I understand correctly, the actual problem the OP is facing is that if the call to javascript:makeajaxcall(v.uID); fails, the creation of the table also fails. So what it appears that the OP actually needs is a way to gracefully continue with the creation of the table, even if an error occurs.

The best way to deal with that is to wrap the contents of the function in a try/catch. If you do that inside the makeajaxcall function, it'd be best.

The code we really need to see is the code in your makeajaxcall function, as that seems to be where the problem lies.

Community
  • 1
  • 1
chris.nesbit1
  • 333
  • 2
  • 9
  • 1
    It's likely that the OP meant to write `onclick="makeajaxcall(v.uID);"` instead of concatenating the function's return value. – JJJ Sep 09 '13 at 17:11
  • 1
    @user2422387 "not working" has no meaning, on Stack Overflow. Are you getting an error message, or is there an error showing in the web/js console of your favorite browser? – chris.nesbit1 Sep 09 '13 at 17:14
  • @Juhana this could be. user2422387 could you clarify if that's the case? – chris.nesbit1 Sep 09 '13 at 17:15
  • hey there is no error it is not working if there is a small problem in ajax call even no error messages do you know @chris.nesbit1 – user2422387 Sep 09 '13 at 17:18
0

This probably doesn't do what you want because of your quotes. You're not ending them when you want and the onclick function should be in HTML quotes. It should look something like this.

tbl_row += "<td><a href='#' onclick='javascript:makeajaxcall(v.uID);'>Delete</a></td>";

That's why in JavaScript and PHP I always use single quote(') and in HTML I always use double quote(").

  • Well that's because your onclick doesn't work. You want it to make an ajax call on the click. The link doesn't know anything about v when it's clicked. I suggest putting in some IDs but if you know this format isn't going to change you could do this `onclick='makeajaxcall(this).parent().first());'` – Gabriel Limon Sep 10 '13 at 17:13