This is super weird... I'm trying to make a validation function for some forms but I can't seem to run any JQuery on the elements in the array JQuery gives me.
This breaks when it hits the .css line. I have similar problems on the .val() and the sceond .css. Whaaaaat?
function isRequiredFilled(prepend){
var reqs = $(prepend + ' .required');
var filled = true;
for(var i=0; i<reqs.length; i++){
reqs[i].css("background-color", "white");
if(!reqs[i].val()){
reqs[i].css("background-color", "rgba(200, 0, 0, 0.7f");
filled = false;
}
}
return filled;
}
var $el = {};
guards against the possibility of calling a jQuery method on something that is not an object and therefore generating an exception. However, in this case, thefor
loop won't run if$reqs.length() === 0
, so you're right thatvar $el;
is sufficient. (2) I declare variables outside of loops because of some misremembered advice about performance, but as per http://stackoverflow.com/questions/3684923/javascript-variables-declare-outside-or-inside-loop, it doesn't seem to matter! Updating the function now. Thanks! – unruthless Apr 29 '14 at 20:12