javascript - Jquery subtracting different values -


i have function have 3 input fields. if input values anywhere on textfield, subtract each textfield has value totalnumber variable.

var totalnumber = 3; var textfieldsleft = 0; var textfieldshasvalue = 0;  <input type = "text" id = "first"> <input type = "text" id = "second"> <input type = "text" id = "third"> 

how can jquery if input input id ="first", value of textfieldleft = 2 , textfieldshasvalue = 1 , totalnumber = 2.

here jquery code

$(this).keyup(function(){   countvalue = $(this).val().length;   //alert(gettext);   if(countvalue != 0){     console.log('yes indeed has value');     $(this).css(colorblue);     //alert(id);       } }); 

i need track down how many fields left has not been filled out yet.

just set common class inputs , perform aforementioned logic on change event.

html:-

<input type="text" id = "first" class="inps"> <input type="text" id = "second" class="inps"> <input type="text" id = "third" class="inps"> 

js:-

var totalnumber = 3; var textfieldsleft = 0; var textfieldshasvalue = 0;   $(".inps").on("change", function(){   textfieldshasvalue = 0;   $(".inps").each(function(){     if($(this).val() !==''){        textfieldshasvalue++;     }   });   textfieldsleft = totalnumber - textfieldshasvalue; }); 

note:- polluting global scope bad practice, , should avoided.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -