How to clear input? [duplicate]

0

NOTE: url and number types mainly but if you want to put others tb without problems)

Preferably in pure Js!

I've tried things like this.value ''; and they did not!

NOTE 2: I've read Removing or clearing value of the input file? and this one had not solved my problem, is talking about input file and uses a trick to solve. I tried to adapt it to my code and it did not solve!

    
asked by anonymous 29.12.2016 / 18:46

1 answer

4

It depends on the event you are invoking, but basically it will work for most input .

Javascript

With javascript you can change the value property:

document.getElementById("limpar").addEventListener("click", function() {
  clearInputUrlNumberText("entrada");
});

function clearInputUrlNumberText(name) {
  var entradas = document.querySelectorAll("input[name='"+name+"']");
  [].map.call(entradas, entrada => entrada.value = '');
}
<input name="entrada" type="url">
<input name="entrada" type="number">
<input name="entrada" type="text">
<br>
<button id="limpar">Limpar</button>

jQuery:

With jQuery it's possible using the val method:

$("button").click(function(){
  $("input[data-name='entrada']").val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputdata-name="entrada" type="url">
<input data-name="entrada" type="number">
<input data-name="entrada" type="text">
<br>
<button id="limpar">Limpar</button>
    
29.12.2016 / 18:48