Home > Mobile >  How do I remove the input typed in by user in an input tag after form submission through vanilla jav
How do I remove the input typed in by user in an input tag after form submission through vanilla jav

Time:10-23

 <input type="number" class="setter" id="day-set" name="day-set" value="day"max="30" min="0" placeholder="00"onkeyup="if(parseInt(this.value)>30){ this.value =30; return false;}">
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

How do I make it so that the input typed in by the user in the input field is cleared when a submit button is pressed (the one which does not refreshes the page)

CodePudding user response:

you can add this to the onclick function of your button and replace the myForm with the id of your form element

document.getElementById("myForm").reset();

CodePudding user response:

   <html>
    <body>
    
      <p>Clear the input field when you click on the button:</p>
      <button onclick="document.getElementById('myInput').value = ''">Clear input 
       field</button>
    
      <input type="text" value="Blabla" id="myInput">
    
    </body>
   </html>

You can also write the javaScript in-between the script tag in a Function or any other way that you want.

CodePudding user response:

You can create a function and add it into the onSubmit event of your form (assuming that you have a form) and then inside of that function you only need to clear the value using something like this:

document.getElementById('day-set').value = ''

Example:

<form onsubmit="myFunction()">
   <input type="number" class="setter" id="day-set" name="day-set" value="day"max="30" min="0" placeholder="00"onkeyup="if(parseInt(this.value)>30){ this.value =30; return false;}">
  <input type="submit">
</form>

<script>
 function myFunction(){
  document.getElementById('day-set').value = ''
 } 
</script>
  • Related