user347935 user347935 - 2 months ago 8
Javascript Question

Dynamic message on onclick confirm() without passing value

Using

a href
, I want to display the value of
itemName
on the message of confirm() but pass
itemId
. Here is the code:

<a href="del.jsp?itemID=<%=itemID%>"
onclick="return confirm('Continue to delete employee: ' +
<%=itemName%> + '?');">
Delete
</a>


But this doesn't work. If I change
itemName
to
itemID
, it works fine. I read that by concatenating, you are passing the value of the variable you concatenated, but what I want to display is the name. Is there any way to do this?

Answer

As that's serverside code, you don't need to concatenate it in javascript, just insert it directly

<a href="del.jsp?itemID=<%=itemID%>"
    onclick="return confirm('Continue to delete employee: <%=itemName%>?');">
    Delete
</a>