0

My code like this :

$('body').on('click', '#add-image', function(e) {
    $(this).find('#form-add').trigger('reset');
    // $(this).find('#form-add').clearForm();
    // $('#form-add').clearForm();
    // $('#form-add').val("");
    // ...
});

If button clicked, I want the form clear

I try like that, but it does not work

How can I solve this problem?

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • Try simply $('#form-add').trigger('reset'), see https://stackoverflow.com/questions/3786694/how-to-reset-clear-form-through-javascript – Zoltan Jun 11 '17 at 04:54

5 Answers5

2

Clearing an entire form with Javascript or JQuery, it's recommended that you
explicitly clear each of your elements.

For example, clearing all text inputs:

$('#txtbox1').val('');
$('#txtbox2').val('');
etc. etc.

Another less recommended alternative is to use reset():

$('#myForm')[0].reset();

But this does not 100% guarantee clearing all elements within, since some of the elements may not be scoped within your form.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • I need you help. Look at this : https://stackoverflow.com/questions/44486251/why-image-not-display-in-modal-bootstrap – moses toh Jun 11 '17 at 23:14
0

Let us suppose you have a button with class clearAll, call the below jquery function which will clear all the textfields and textarea at once...easier and simple...customize however you want...

$(".clearAll").click(function() {
    $(this).closest('form').find("input[type=text], textarea").val("");
});
utkarsh31
  • 1,439
  • 2
  • 13
  • 20
0

Yes you can reset a form, is almost the same as clean.

Pure JS:

document.getElementById('THE-FORM-ID').reset();

DEMO: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset

Bitito
  • 191
  • 6
0

Try with empty() function jquery

$('body').on('click', '#add-image', function(e) {
    $(this).find('#form-add').trigger('reset');
     $(this).find('#form-add').find('input,textarea').empty()
});
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • I need you help. Look at this : https://stackoverflow.com/questions/44486251/why-image-not-display-in-modal-bootstrap – moses toh Jun 11 '17 at 23:15
0

You can clean all the text fides that undet that form like that :

$("#clean").click(function() {
    $(this)
    .closest('form')
    .find("input[type=text],textarea")
    .val('');
});
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72