ComeRun
ComeRun

Reputation: 921

Passing a java variable to a javascript function

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?

Upvotes: 2

Views: 6361

Answers (2)

Ben McCormick
Ben McCormick

Reputation: 25728

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;
   }
}

Upvotes: 0

sasi
sasi

Reputation: 4318

 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..

Upvotes: 1

Related Questions