Is there a reason that this code:
var verb = "";
function printPage (input) {
$("div").append(input + "<br>");
}
$( "#submit" ).click(function() {
verb = $('#enter').val();
printPage(verb);
});
printPage(verb);
only results in #enter (a form)'s value being logged once in the click event (while the last printPage prints the original empty string), while this code:
var verb = "";
function printPage (input) {
$("div").append(input + "<br>");
}
function buttonClick() {
verb = "verb!";
printPage(verb);
}
buttonClick();
printPage(verb);
prints verb twice, as the function has successfully changed the verb variable value? Sorry if I haven't explained my problem well enough, I'll elaborate more if needed.
$( "#submit" ).click(...)will be triggered only when you click the button. Whereas in the second case, since you are explicitly callingbuttonClick(), it'll log twice.var xin global scope to make something global (though var is not strictly needed). More importantly in this case, it need not be global, it can be limited to the parent scope, which I believe should be the function bound to$(document).ready()