javascript - Calling a function from within nested functions in jQuery -
javascript - Calling a function from within nested functions in jQuery -
i'm guessing scope problem, can't work out! have function validates form before sending. on submit, iterates through each field , checks details. should set cookie each value, since form sends user away site, temporarily, , need info when return.
i have set cookie function works fine, calling function within 2 nested functions nothing.
$(document).ready(function() { $("form#mainrequestbrochure").submit(function(event) { event.preventdefault(); //prevent form.submit() var missingdata = 0; // missing required fields counter $("form#contactform input").each(function(index) { var ok = 0; var v = $(this).val(); var n = $(this).attr('name'); if(isblank(v)===true) { // if empty $(this).addclass("error"); // add together error class missingdata++; // add together 1 counter } else if(($(this).attr('type')==="email")&&(!isvalidemailaddress(v))) { $(this).addclass("error"); missingdata++; } else if ($(this).hasclass("error")) { // not empty has had error class applied $(this).removeclass("error"); // clear error class ok = 1; } else { ok = 1; } if(ok===1) createcookie(n,v,7); }); createcookie('testing2','test',7); // if no errors recorded, submit form if(missingdata === 0 ) $("form#mainrequestbrochure").submit(); }); }); function createcookie(name,value,days) { if (days) { var date = new date(); date.settime(date.gettime()+(days*24*60*60*1000)); var expires = "; expires="+date.togmtstring(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; alert('cookie ' + name + ' set.'); }
the code straight within of $("form#mainrequestbrochure").submit(function(event)
works fine, creating cookie named testing2 using createcookie('testing2','test',7)
, when place same line of code within .each()
loop, nothing.
like say, i'm guessing scope thing, it's driving me wall....
move createcookie function outside of submit function.
javascript jquery scope each
Comments
Post a Comment