I got this
$('#password').change(function() {
var toSha1 = $('#msisdn').val() + $('#password').val();
var authCode = $.sha1(toSha1);
});
How can I make the variable authCode to be visible in the whole script. For example this to work properly:
$('#password').change(function() {
var toSha1 = $('#msisdn').val() + $('#password').val();
var authCode = $.sha1(toSha1);
});
alert(authCode);
I’ve tried by defining it without the keyword “var” but it seemed not to work.
EDITED: here’s the source
<div data-role="content">
<textarea id='resultArea'></textarea>
<label for='msisdn'>MSISDN:</label>
<input type='text' id='msisdn' value='+359899888777'>
<label for='authCode'>authCode:</label>
<input type='text' id='authCode' value='8bcac5dabf06219843a5a3b755c47e69600e050a'>
<label for='password'>Password:</label>
<input type='password' id='password' value='123'>
<button data-role='button' data-inline='true' data-theme='e' id='register'>Register</button>
<button data-role='button' data-inline='true' data-theme='e' id='login'>Login</button>
</div><!-- /content -->
<script>
$('#resultArea').hide();
$('#password').change(function() {
var toSha1 = $('#msisdn').val() + $('#password').val();
window.authCode = $.sha1(toSha1);
});
alert(window.authCode);
</script>
To make it a global explicitly:
Global variables are properties of the
windowobject.Note that there’s a separate issue with your code, though: You’re alerting the value of
authCodeimmediately after hooking up the handler, rather than when thechangeevent fires. See the comments: