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 form
is. You need to specify document.forms[0].ip.value
instead.
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.form
for onClick of anchor tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With