How to clear multiple input fields by classes?

Asked

Viewed 107 times

0

How to clear the value of multiple input fields by classes? Using Jquery?

I did so, but did not fuck!

form.find('input').not(".class1 .class2 .class3").val(''); 

  • Fill the tags correctly, what language are you using? Pure Javascript? Jquery?

  • How is your form? cite examples

  • It may be duplicated from https://answall.com/q/341638/6333, which you yourself are the author.

  • 'Cause you’re asking the same question?

  • 1

2 answers

0

From what you can see by your code you’re using the function .val(), then it should probably be Jquery Just search for your input and use val().

$('.class1').val('')

To take several at once and clear the value by following the following logic:

$("input[type='text']").each(function() {
  $(this).val('');
});

Any doubt I’m available.

  • So far so good, with a class inside, you can perform this function quietly, but there’s no way I can put more than one class inside the dial ?

  • They have some Id in common?

  • No, just different classes.

  • Set an Id equal to them then and use the logic of the second example I gave in the answer, replacing "input[type='text']" with "input[id='text']"

0

I don’t understand why you used the method not(), because if you want to clear the fields with the classes, if you put not() you will do exactly the opposite. You can call as many classes as you want, just separating them by a comma. In the example I cleaned the inputs with the class Class1, class2 and class3:

$(function(){
  $('.class1, .class2, .class3').val('');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input type="text" class="class1" value="Input 1">
  <input type="text" class="class2" value="Input 2">
  <input type="text" class="class3" value="Input 3">
  <input type="text" class="class4" value="Input 4">
  <input type="text" class="class5" value="Input 5">
</form>

In the case of the use of not(), this way the use, you choose the input to be the selector and classes that do not want inputs to be cleaned. In the example I want the classes Class1 and class2 DO NOT have your inputs cleaned:

$(function(){
  $('input').not('.class1, .class2').val('');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input type="text" class="class1" value="Input 1">
  <input type="text" class="class2" value="Input 2">
  <input type="text" class="class3" value="Input 3">
  <input type="text" class="class4" value="Input 4">
  <input type="text" class="class5" value="Input 5">
</form>

  • Actually it’s the other way around, I don’t want to clean up where those classes are! Because they are buttons where the person clicks to register, and after the registration, their value goes.

Browser other questions tagged

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