mrpatg
mrpatg

Reputation: 10117

clear textbox after form submit

I have a form which posts using ajax and reloads the div underneath.

I need the textbox to clear when the submit button is pressed.

<form name=goform action="" method=post>
<textarea name=comment></textarea>
<input type=submit value=submit>
</form>

Upvotes: 4

Views: 49473

Answers (4)

Hrushi
Hrushi

Reputation: 309

<script>
    function testSubmit()
    {
        var x = document.forms["myForm"]["input1"];
        var y = document.forms["myForm"]["input2"];
        if (x.value === "")
        {
            alert(' fill!!');
            return false;
        } Blockquote
        if(y.value === "")
        {
            alert('plz fill the!!');
            return false;
        }
        return true;
    }
    function submitForm()
    {
        if (testSubmit())
        {
            document.forms["myForm"].submit(); //first submit
            document.forms["myForm"].reset(); //and then reset the form values
        }
    } </script> <body>
    <form method="get" name="myForm">

        First Name: <input type="text" name="input1"/>
        <br/>
        Last Name: <input type="text" name="input2"/>
        <br/>
        <input type="button" value="Submit" onclick="submitForm()"/>
    </form>

</body>

Upvotes: 0

Ozzy
Ozzy

Reputation: 1730

If you're not using jQuery (which is required for the solutions given above) you can replace the

$("#txtComment").val("");

with

document.getElementById("txtComment").value = "";

Upvotes: 2

mauris
mauris

Reputation: 43619

Simply call this after posting:

$("textarea[name=comment]").val("");

Or to improve, assign an ID to your textarea:

<form name=goform action="" method=post>
<textarea id="txtComment" name="comment"></textarea>
<input type=submit value=submit>
</form>

and use this after posting:

$("#txtComment").val("");

Upvotes: 0

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

Add an id for the textarea.

<textarea name='comment' id='comment'></textarea>

Hook into the submit process and add:

$('#comment').val('');

Upvotes: 20

Related Questions