Can someone please help me with this. I what to return var from input event function to outer function.
function takeInputText() {
var input = document.getElementById('inputText')
var newText;
input.onkeyup = function(e) {
var text = input.value;
if ( e.which == 13 ) {
newText = text;
input.value = '';
//return newText - I want to return 'newText' to 'takeInputText()' so i can
// use 'takeInputText()' as a variable
}
}
}
takeInputText();
newTextvariable can be accessible from thetakeInputText()method also.takeInputTextbe passed to?onkeyupfunction is asynchronous. It isn't called at the same time as your other code, it is called later when the user hits and releases a key. So you can't "return" a value, because there is nothing to return it to. Instead, as shown in the various answers, to usenewTextsomewhere else in your code, you have to call a function and pass it as an argument to that function.