2

I have the following piece of code which is a input content for a js lib:

content:'<a href="x.com" onclick="confirmDel("<%=user.name%>")" > <img src="delete.png" alt="Delete" width="15px" height="15px"></a></div>'

confirmDel(name)
{
 if(confirm("delete " + name +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}

when I click the delete link, it does not prompt me with the delete confirmation alert! what am I missing here? Is it about the way I pass the variable to the function?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
ComeRun
  • 921
  • 1
  • 20
  • 38

2 Answers2

1
 content:'<a href="x.com" onclick="confirmDel(\''+<%=user.name%>+'\')" > <img src="delete.png" alt="Delete" width="15px" height="15px"></a></div>'

problem with passing parameter in the onclick..use escape character..

sasi
  • 4,192
  • 4
  • 28
  • 47
  • No this should work fine, its not broken, and actually will break if you do it this way. It will try to treat the name as a variable rather than a string. – Ben McCormick Mar 06 '13 at 03:58
  • It would be better without scriptlets usage since is deprecated. Check [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Mar 06 '13 at 04:05
0
confirmDel(name)
{
 if(confirm("delete " + name +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}

This is incorrect. Its halfway between declaring a function and calling it, but doesn't really do either.

You need to declare the function and then call it to execute that code. You're calling it in the onclick section now, but you need to define it correctly

function confirmDel(nameStr)
{
 if(confirm("delete " + nameStr +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71