0

I have a form that utilizes the Jquery form plugin. When the form is submitted I need to clear a textfield on my page. When this Javascript is run I want it clear the Textfield after running the php page.

<script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $("#glue").ajaxForm({url: 'scripts/glue.php', type: 'post'})
    }); 
</script> 

This is my input field:

<input type="text" id="glueField" class="form-control" name="word" placeholder="Press enter to glue a word" autocomplete="off" autofocus>

6 Answers6

2

I would use this method from the jQuery Form plugin, which clears all text inputs, password inputs, etc. inside of your ajax form. Like this:

$("#glue").ajaxForm({url: 'scripts/glue.php', type: 'post'}, function() {
    // Callback for when post is completed
    $('#glue').clearForm();
});    

More info here

Alec Moore
  • 1,155
  • 2
  • 10
  • 20
1

Try with:

$("#glue").ajaxForm({url: 'scripts/glue.php', type: 'post'}, function(){
  $('#glueField').val('');
});
hsz
  • 148,279
  • 62
  • 259
  • 315
0

If you are using inputs in that #glue then, after your ajax call, add this line would do the trick for you:

$('input').empty();
Faron
  • 1,255
  • 11
  • 20
0

I fixed it myself. The jQuery form plugin has a method of doing this. I simply changed my jquery line to this:

$("#glue").ajaxForm({url: 'scripts/glue.php', type: 'post', clearForm: 'true'}, function() {

Adding:

clearForm: 'true'
-1
jQuery (textFieldId/class).val ("");
-1
$("#glue").ajaxForm({url: 'scripts/glue.php', type: 'post', success: $("#glueField").val(''))})

You can do it like this. Set an empty string to the respective field on success.

Muneeb Zulfiqar
  • 1,003
  • 2
  • 13
  • 31