This works:
$(document).ready(function(){
$("#inputbuttonid").on("click", function(){
var inputtext = $("#inputtextid").val();
alert(inputtext);
});
});
But this doesn't work:
$(document).ready(function(){
var inputtext = $("#inputtextid").val();
$("#inputbuttonid").on("click", function(){
alert(inputtext);
});
});
By "works" I mean it alerts the correct information.
The only difference in the above 2 cases is that in the second, I put
var inputtext = $("#inputtextid").val();
in the outer function, whereas in the first, I put it in the inner function.
According to the JQuery manual, "local scope works through functions", meaning "any functions defined within another have access to variables defined in the outer function" (quoted from the JQuery manual).
So, why in the second case above, does the inner function not have access to "inputtext"?