How to clear multiple input fields by classes?

0

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

I did that, but it did not work!

form.find('input').not(".class1 .class2 .class3").val(''); 
    
asked by anonymous 13.11.2018 / 10:20

2 answers

0

From what you can see from your code you are using the function .val() , so it should probably be JQuery Just search your input and use val ().

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

To take several at a time and clear the value following the following logic:

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

Any questions are available.

    
13.11.2018 / 10:30
0

I do not understand why you used the not () method, because if you want to clear the fields with the classes, if you put not () will do exactly the opposite. You can call as many classes as you want by just separating them with 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><inputtype="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 using not () , this would be the use, you choose input to be the selector and classes that do not want the inputs to be cleaned. In the example I want the class1 and class2 classes NOT to have their inputs cleaned up:

$(function(){
  $('input').not('.class1, .class2').val('');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form><inputtype="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>
    
13.11.2018 / 14:28