Jquery clear values โ€‹โ€‹of all input fields except one

I am trying to write a script that clears the value of all input fields except one. This script still clears the DESCRIPTION input field.

jQuery('.campaign-column').not('active').each(function(index){ if(jQuery('.campaign-column input[name!="DESCRIPTION"]')){ jQuery(this+':input').val(''); } }); 
+4
source share
2 answers

Simplify:

 $('.campaign-column:not(.active) input:not([name="DESCRIPTION"])').val('') 

I assume that you meant .not('.active') and not .not('active') , since there is no <active> element in HTML. Note: the loop ( .each() ) in your source code is pointless, as each iteration selects the same elements over and over again.

+8
source

In this case, you do not need to write each cycle. try it

 $(function(){ // Select all text fields but not Description field.. var $txtfields = $('.campaign-column:not(.active)').find('input[type=text][name!=DESCRIPTION]') $('#btn1').on('click', function() { $txtfields.val('') ; }); });โ€‹ 

Check FIDDLE for a working example.

+1
source

Source: https://habr.com/ru/post/1432988/


All Articles