I am trying to pass variables on the onclick event to a Javascript function. I am trying the following way, I can’t get the input value in the Javascript function. (I am expecting an alert of 1.) Is it the right way of doing this? Please help.
<html>
<head>
<script>
function submit_value(form) {
alert(form.ip.value);
}
</script>
</head>
<table>
<tr>
<form>
<td>
<input id="ip" type="text" value="1">
</td>
<td>
<a href="javascript:;" onClick="submit_value(this)">Button</a>
</td>
</form>
</tr>
</table>
</html>
Your script doesn’t know what
formis. You need to specifydocument.forms[0].ip.valueinstead.If there are more than one form on the document then it will be better if you store the form element in variable first. You can have an id on the form for that…
<form id="formID">and in submit_value function you can have
var myForm = document.getElementById('formID');alert(myForm.ip.value);
Edit:
You can use
this.formfor onClick of anchor tag.