Uncaught Referenceerror: das is not defined at Htmlanchorelement.onclick

Asked

Viewed 705 times

0

I’m having a problem with a function call. A table shows the information of a client, and when clicking it should call a function that would show an Alert on the screen with the client’s CPF.

out.println("<table border='1' width=300px>");

for (Cliente cli : lista) {
    out.println("<tr>");

    out.println("<td><a onclick='getCli(" + cli.getCpfCli().toString() +  ");'> " + cli.getNomeCli() +"</a></td>");

    out.println("</tr>");
}

out.println("</table>");

function getCli(a){
   var cpf = a;
   $.ajax({
       url: "ajax/getcliente.jsp",
       type: "GET",
       data: {
               cpf : cpf
             },
       success: function (data) {
                   comerc = $(data).find("comercial").text();
                   alert(comerc);
                }
   });

I am using ajax.

1 answer

0

part of the error is in function called js in element

[...] getCli(" + cli.getCpfCli().toString() +  "); [...]

as it is a variable string, need to pass between quotes, in your code, is passing

[...] getCli(abc); [...]

Try

for (Cliente cli : lista) {
    out.println("<tr>");

    out.println("<td><a onclick='getCli(\'" + cli.getCpfCli().toString() +  "\'');'> " + cli.getNomeCli() +"</a></td>");

    out.println("</tr>");
}
out.println("</table>");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.