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?

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
It would be better without scriptlets usage since is deprecated. Check How to avoid Java Code in JSP-Files?
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;
   }
}

1 Comment

your function declaration still won't work as is though. You need to add the function keyword in there somewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.