1

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();
Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
Edy Mrkos
  • 43
  • 1
  • 4
  • What's your problem ? `newText` variable can be accessible from the `takeInputText()` method also. – Kalpesh Rajai Jun 30 '16 at 16:36
  • This isn't a duplicate, but a good hint is [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 4castle Jun 30 '16 at 16:38
  • What you want to do with that input ? I assume you are going by wrong direction... – Rayon Jun 30 '16 at 16:40
  • _"I want to return 'newText' to 'takeInputText()' so i can // use 'takeInputText()' as a variable"_ What would `takeInputText` be passed to? – guest271314 Jun 30 '16 at 16:51
  • To help you understand the answers, what you want to do is not possible in the form you're asking about. The `onkeyup` function 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 use `newText` somewhere else in your code, you have to *call a function* and pass it as an argument to that function. – Michael Geary Jun 30 '16 at 17:56

2 Answers2

3

If you are willing to play with the value of the input once user hit enter key, bind keyup event over input and manipulate the value in the callback function. You can also call another function in which value of the input could be passed.

Example:

var input = document.getElementById('inputText');
input.onkeyup = function(e) {
  var code = e.which || e.keyCode;
  if (code == 13) {
    appendInput(this.value);
    this.value = '';
  }
}

function appendInput(value) {
  var elem = document.createElement('h2');
  elem.textContent = value;
  document.getElementById('preview').appendChild(elem);
}
<input type="text" id="inputText">
<div id="preview"></div>
Rayon
  • 36,219
  • 4
  • 49
  • 76
1

You cannot use takeInputText() as a variable since what you are doing inside takeInputText is just attaching a keyup listener to the input element and the function returns immediately (undefined is returned in this case).

To make use of the value, pass the variable as an argument to a function that you want to invoke when the condition inside the handler is true, for example:

input.onkeyup = function(e) {
    var text = input.value;
    if ( e.which == 13 ) {
        input.value = '';
        yourFunction(text);
    }
}
Mars Cheng
  • 31
  • 3