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.9k16 gold badges160 silver badges355 bronze badges
asked Mar 6, 2013 at 3:51
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..

answered Mar 6, 2013 at 3:52
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;
 }
}
answered Mar 6, 2013 at 3:59

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.