Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the parent form on the onclick event to javascript function

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>
like image 320
user1051505 Avatar asked Dec 18 '12 04:12

user1051505


1 Answers

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.

like image 52
Faisal Sayed Avatar answered Oct 21 '22 16:10

Faisal Sayed