How to clean input?

Asked

Viewed 3,366 times

0

OBS.: url and number types mainly but if you want to put of other tb without problems)

Preferably in pure Js!

I’ve tried things like this.value'; and they haven’t solved!

OBS 2 : I have read the How to remove or clear input file value? and this 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 didn’t solve!

  • The question was unclear after remark 2 @Giancarlosolderagrazino. Could provide relevant snippets of your code and/or some example?

  • Try to edit your post with your js code, so we understand your problem.

1 answer

4


It depends on the event it will invoke, but basically it will work for most input.

Javascript

With javascript it is possible to change the value of the property value:

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 is possible using the method val:

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

  • I think a solution using jQuery would be better, since it added this tag and probably uses this lib.

  • He emphasized pure js, but anyway is good the tip. Thanks @mauhumor

  • 1

    very cool the way you work with javascript +1

  • Can you make the name be passed by parameter in a function? Look at my try: Function clearInputUrlNumberText(){ var inputs = Document.querySelectorAll("input[name='in3']"); [].map.call(inputs, input => input.value = '); }

  • There is @Giancarlosolderagrazino. I edited with the example of your attempt, see if this is it.

  • 1

    saved my day! Thank you very much!

  • I’m happy to help! You can mark the answer as accepted if you want @Giancarlosolderagrazino :)

  • I hadn’t noticed that the guy asked in pure JS. So ignore my comment.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.