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
Post a Comment