javascript - Input's value as variable is not defined -


got input type text. whatever entered supposed become value variable, , further on there. yet, error "uncaught referenceerror: emailvar not defined" , whole script breaks there.

html

<input type="text" class="signfield emfield" /> <div class="submt sbmtfrm" href="#" style="cursor:pointer;">step 2</div> 

and js

$(".sbmtfrm").click(function(){    var emailvar = $(".emfield").val(); }); 

in code, emailvar being defined in function closure, , function has access it.

$(".sbmtfrm").click(function(){    var emailvar = $(".emfield").val(); }); 

if want use emailvar outside of jquery event handler, need first define (not assign it, yet) outside scope of function.

(function(window, $) { // closure     var emailvar;      $(".sbmtfrm").click(function() {         emailvar = $(".emfield").val();     });      // have access `emailvar` in function in closure  }(window, jquery)); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -