1

Currently, the following form fields are being cleared out by the jQuery shown:

HTML form:

<form action="" method="post" id="add">
    <legend>Add Mobile</legend>
    <ol>
        <li>
        <label for="add_name">name</label>
            <input type="text" name="name" id="add_name"></input>
        </li>
        <li>
            <label for="add_model">model</label>
            <input type="text" name="model" id="add_model"></input>
        </li>
        <li>
            <label for="add_color">color</label>
            <input type="text" name="color" id="add_color"></input>
        </li>                               
    </ol>
    <button type="button" onclick="addMobile()">Submit</button>
    <button type="reset">Cancel</button>
</form>

jQuery:

// clear out form fields
$('#add input[name="name"]').val('');
$('#add input[name="model"]').val('');
$('#add input[name="color"]').val('');

Is there a way to write a selector such that all inputs of type "text" in the form with an ID of "add" will be cleared out?

9/22/2019 edit: The form fields get cleared out by jQuery as part of the addMobile() function in an AJAX call. So, if the add is successful part of the .done logic is to clear out the form fields. The purpose of the Cancel button is to give the user a way to clear out the fields manually if desired.

knot22
  • 2,648
  • 5
  • 31
  • 51

4 Answers4

1

Remove type="reset" attribute. Because that attibute prevents you.

This method assigns the desired value to all text fields within the form you specify.

<form action="" method="post" id="add">
    <legend>Add Mobile</legend>
    <ol>
        <li>
        <label for="add_name">name</label>
            <input type="text" name="name" id="add_name"></input>
        </li>
        <li>
            <label for="add_model">model</label>
            <input type="text" name="model" id="add_model"></input>
        </li>
        <li>
            <label for="add_color">color</label>
            <input type="text" name="color" id="add_color"></input>
        </li>                               
    </ol>
    <button type="button" onclick="addMobile()">Submit</button>
    <button onClick="ClearTextInputs('add')">Cancel</button>
</form>

<script>
function ClearTextInputs(formId){
    //$("#"+formId+" input:text").val("You Value"); //New Value
      $("#"+formId+" input:text").val(""); //Remove value
    }
</script>
Fatih GÜRDAL
  • 1,489
  • 1
  • 17
  • 19
0

If you add class to the input tags you want to clear then, It would be quite simple as below. You can also use conditional cases to clear the values inside the forms.

HTML Code:

<form action="" method="post" id="add">
    <legend>Add Mobile</legend>
    <ol>
        <li>
        <label for="add_name">name</label>
            <input type="text" class="clearMe" name="name" id="add_name" value="rarara"/>
        </li>
        <li>
            <label for="add_model">model</label>
            <input type="text" class="clearMe" name="model" id="add_model" value="bababa"/>
        </li>
        <li>
            <label for="add_color">color</label>
            <input type="text" class="clearMe" name="color" id="add_color" value="tataata"/>
        </li>                               
    </ol>
    <button type="button" onclick="addMobile()">Submit</button>
    <button type="reset">Cancel</button>
</form>

Jquery:

$('.clearMe').val('');

jsfiddle.

Jennis Vaishnav
  • 331
  • 7
  • 29
0

I ended up using this: $('#add').trigger('reset');

See this link, specifically the answer from @user768680 dated Mar 17 '13 at 18:45.

knot22
  • 2,648
  • 5
  • 31
  • 51
0

You can directly get group of input[type=text] and reset all together.

HTML CODE:

<form action="" method="post" id="add">
    <legend>Add Mobile</legend>
    <ol>
        <li>
        <label for="add_name">name</label>
            <input type="text" name="name" id="add_name"></input>
        </li>
        <li>
            <label for="add_model">model</label>
            <input type="text" name="model" id="add_model"></input>
        </li>
        <li>
            <label for="add_color">color</label>
            <input type="text" name="color" id="add_color"></input>
        </li>                               
    </ol>
    <button type="button" onclick="addMobile()">Submit</button>
    <button onClick="clearMe()">Cancel</button>
</form>

JS:

function clearMe() { 
    $('form').find("input[type=text]").reset();
}
Sam
  • 404
  • 1
  • 4
  • 18