250

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>");
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
kosbou
  • 3,919
  • 8
  • 31
  • 36

8 Answers8

497

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

shaunsantacruz
  • 8,903
  • 3
  • 19
  • 19
  • 3
    Does this work and is is it specification compliant if we have a numeric input like so ``? I guess said differently, are you allowed to set a numeric input to be blank? – Kevin Wheeler Jun 10 '15 at 21:49
  • 3
    How about `.val([])`? – Fr0zenFyr Sep 14 '15 at 19:36
  • 3
    On a related note, `.val([])` can also unselect any selected option in select list... not so much with `.val('')`. Best thing about it is it's cross-browser support. **P.S:** This is an alternative to only other efficient cross-browser solution - `$("select option").prop("selected", false);` but works with all inputs. Thanks to [Jon](http://stackoverflow.com/users/50079/jon) for [a test he created](http://jsfiddle.net/jS3ws/). – Fr0zenFyr Sep 14 '15 at 19:49
36
$(document).ready(function(){
  $('#shares').val('');
});
osahyoun
  • 5,173
  • 2
  • 17
  • 15
34

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);
chickens
  • 19,976
  • 6
  • 58
  • 55
15

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>
Radhika
  • 189
  • 1
  • 6
9

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

Clear the input field when the page loads:

$(function(){
    $('#shares').val('');
});
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
5

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

$(document).ready(function(){
  $('#shares').trigger(':reset');
});
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
0

If the input is pre-filled (e.g. in a submitted search form), you have to clear the value-attribute as well:

$("#clearSearch").on('click', function(){
  $('#search').val('').attr('value','');
});
Yoche2001
  • 56
  • 4
0

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>");
            });
chris
  • 36,115
  • 52
  • 143
  • 252