How to empty input field with jQuery

JqueryInputNumbersField

Jquery Problem Overview


I am in a mobile app and I use an input field in order user submit a number.

When I go back and return to the page that input field present the latest number input displayed at the input field.

Is there any way to clear the field every time the page load?

$('#shares').keyup(function(){
	payment = 0;
	calcTotal();
	gtotal = ($('#shares').val() * 1) + payment;
	gtotal = gtotal.toFixed(2);
	$("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
});

Jquery Solutions


Solution 1 - Jquery

You can clear the input field by using $('#shares').val('');

Solution 2 - Jquery

$(document).ready(function(){
  $('#shares').val('');
});

Solution 3 - Jquery

To reset text, number, search, textarea inputs:

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

To reset select:

$('#select-box').prop('selectedIndex',0);

To reset radio input:

$('#radio-input').attr('checked',false);

To reset file input:

$("#file-input").val(null);

Solution 4 - Jquery

While submitting form use reset method on form. The reset() method resets the values of all elements in a form.

$('#form-id')[0].reset();

OR 

document.getElementById("form-id").reset();

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset

$("#submit-button").on("click", function(){
       //code here
       $('#form-id')[0].reset();
});

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form id="form-id">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
  <br><br>
  <input id="submit-button" type="submit" value="Submit">
</form> 
</body>
</html>

Solution 5 - Jquery

Setting val('') will empty the input field. So you would use this:

Clear the input field when the page loads:

$(function(){
    $('#shares').val('');
});

Solution 6 - Jquery

Since you are using jQuery, how about using a trigger-reset:

$(document).ready(function(){
  $('#shares').trigger(':reset');
});

Solution 7 - Jquery

if you hit the "back" button it usually tends to stick, what you can do is when the form is submitted clear the element then before it goes to the next page but after doing with the element what you need to.

    $('#shares').keyup(function(){
                payment = 0;
                calcTotal();
                gtotal = ($('#shares').val() * 1) + payment;
                gtotal = gtotal.toFixed(2);
                $('#shares').val('');
                $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
            });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkosbouView Question on Stackoverflow
Solution 1 - JqueryshaunsantacruzView Answer on Stackoverflow
Solution 2 - JqueryosahyounView Answer on Stackoverflow
Solution 3 - JquerychickensView Answer on Stackoverflow
Solution 4 - JqueryRadhikaView Answer on Stackoverflow
Solution 5 - JqueryFrancis LewisView Answer on Stackoverflow
Solution 6 - JquerySablefosteView Answer on Stackoverflow
Solution 7 - JquerychrisView Answer on Stackoverflow