call javascript function on the click event of hyperlink
html, javascript, jsp, servlets
Solution
<td> <a href="#" onclick="getValues('<%=data[i][0]%>','<%=data[i][1]%>','<%=data[i][2]%>');" >Edit</a></td>
function getValues(pID,protID,eq)
{
document.getElementById('txtPlanID').value=pID;
document.getElementById('txtProtID').value=protID;
document.getElementById('txtSeqNo').value=eq;
//show("block");
return false;
}
simply pass values while calling javascript get use it
Problem
I am developing a web application using JSP and Servlet. I am trying to call javascript function on the click event of hyperlink, and with that I am also passing some parameters to servlet using query string. ``` <td> <a href="#?id=<%=data[i][0]%>&protID=<%=data[i][1]%>&seqNo=<%=data[i][2]%>" onclick="getValues();" >Edit</a></td> ``` javascript function: ``` <script> function getValues() { var url = document.URL; var planID = url.split("="); var pID = planID[1].split("&"); var remURI = url.split("&"); var protID = remURI[1].split("="); var s = remURI[2].split("="); document.getElementById('txtPlanID').value=pID[0]; document.getElementById('txtProtID').value=protID[1]; document.getElementById('txtSeqNo').value=s[1]; //show("block"); return false; } </script> ``` But the problem is that I have to click twice on hyperlink to get desired result. I think onClick event is executing before sending query string. Please let me know if there is anything wrong in source code. Thanks in advance.....