javascript - jQuery get input val() from $("input") array -


i have function returns whether or not every text input in form has value.

when first made function looked this:

function checkinput(inputid) { check = 0;          //should 0 if inputs filled out (var i=0; < arguments.length; i++) {   // of arguments (input ids) check     var ival = $("#"+arguments[i]).val();    if(ival !== '' && ival !== null) {     $("#"+arguments[i]).removeclass('input-error');   }   else {     $("#"+arguments[i]).addclass('input-error');     $("#"+arguments[i]).focus(function(){       $("input").removeclass('input-error');       $("#"+arguments[i]).off('focus');     });   check++;   }  }    if(check > 0) {     return false; // @ least 1 input doesn't have value   }   else {     return true; // inputs have values   }  } 

this worked fine, when called function have include (as arstrong textgument) id of every input wanted checked: checkinput('input1','input2','input3').


trying have function check every input on page without having include every input id.

this have far:

function checkinput() {   var inputs = $("input");   check = 0;     (var i=0; < inputs.size(); i++) {       var ival = inputs[i].val();    if(ival !== '' && ival !== null) {     inputs[i].removeclass('input-error');   }   else {     inputs[i].addclass('input-error');     inputs[i].focus(function(){       $("input").removeclass('input-error');       inputs[i].off('focus');     });   check++;   }  }  if(check > 0) {   return false; } else {   return true; }  } 

when call function returns error:

uncaught typeerror: inputs[i].val not function

what doing wrong?

when inputs[i], returns html element, no longer jquery object. why no longer has function.

try wrapping $() $(inputs[i]) jquery object, , call .val() like:

 $(inputs[i]).val() 

if going use in loop, set variable:

var $my_input = $(inputs[i]) 

then continue use within loop other methods:

$my_input.val() $my_input.addclass() 

etc..


Comments