2

Trying to reset value of input to blank using button and jquery, but it gives

Uncaught TypeError: $(...).reset is not a function

$(document).ready(function() {
  $("#reset_btn").click(function() {
    $("#buy").reset();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input type="number" class="form-control" id="buy" placeholder="Buy Amount" name="buy">

<button type="button" class="btn btn-secondary" id="reset_btn">RESET</button>

3 Answers 3

6

One option to remove the value of input is to use .val(""). Basically setting the value to an empty string.

Here is an example:

$(document).ready(function() {
  $("#reset_btn").click(function() {
    $("#buy").val("");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input type="number" class="form-control" id="buy" placeholder="Buy Amount" name="buy">

<button type="button" class="btn btn-secondary" id="reset_btn">RESET</button>

Sign up to request clarification or add additional context in comments.

3 Comments

Run jsfiddle.net/hgf8x4b7, why #tot_amnt is being reset after clicking twice the reset button?
Thanks, it worked! Does multiple loading of php file (no database connection in it) causes high CPU Usage?
Happy to help. On your other question, It depends on the file you are loading and the processes that it is doing. You have to monitor your app/site for that one :)
2

As the error message clearly states, reset() is not a jQuery method. You'll want to call the native form element's reset() instead. For this, of course, you'll need a form element in the first place.

$(document).ready(function() {
  $("#reset_btn").click(function() {
    $("#form")[0].reset();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<form id="form">
  <input type="number" class="form-control" id="buy" placeholder="Buy Amount" name="buy">

  <button type="button" class="btn btn-secondary" id="reset_btn">RESET</button>
</form>

1 Comment

Your answer also worked but my input tag was not in the form, that's why accepted Eddie's answer.
0
$('#buy').val('');

Clear the value instead of using reset.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.