最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the value inside addEventListener? - Stack Overflow

programmeradmin1浏览0评论

This code seems to be the only one to output which keycode has been pressed on Android devices:

var input_field = document.getElementById('photo-id');

input_field.addEventListener('textInput', function(e) {
    var char = e.data;
    var keyCode = char.charCodeAt(0);

    return true;
});

I need the result value of keyCode outside of this function. I tried return keyCode; instead of return true; but this doesn't work.

How can I achieve this?

EDIT: Detailed explanation.

I'm using TagIt () and I'm trying to modify the code inside a function this.tagInput.keydown(function(event) { which checks if a ma or space has been pressed to seperate words into tags. I don't know how excatly to pass it to this function or where to put the addEventListener? Like I mentioned I just need to make this addition to recognize Android device keycodes.

EDIT2: Code stripped down.

this.tagInput.keydown(function(event) { // function of the library that checks every keydown event

         // my addition to check for Android keycode input:

                try {
                    var last_key_code_pressed;
                    this.addEventListener('textInput', function(e) {
                        var char = e.data;
                        var kc = char.charCodeAt(0);
                        last_key_code_pressed = kc;
                        console.log(kc); // works to output keycode here but not outside

                        return true;
                    });
                    console.warn(last_key_code_pressed);
                } catch(e) {
                    console.warn("error:" + e);
                }


        // need to get the keycode of the addEventListener HERE


        /* here are if-else-conditions to check if ma, 
        enter or space is pressed (code of the mentioned library) */

This code seems to be the only one to output which keycode has been pressed on Android devices:

var input_field = document.getElementById('photo-id');

input_field.addEventListener('textInput', function(e) {
    var char = e.data;
    var keyCode = char.charCodeAt(0);

    return true;
});

I need the result value of keyCode outside of this function. I tried return keyCode; instead of return true; but this doesn't work.

How can I achieve this?

EDIT: Detailed explanation.

I'm using TagIt (http://aehlke.github.io/tag-it) and I'm trying to modify the code inside a function this.tagInput.keydown(function(event) { which checks if a ma or space has been pressed to seperate words into tags. I don't know how excatly to pass it to this function or where to put the addEventListener? Like I mentioned I just need to make this addition to recognize Android device keycodes.

EDIT2: Code stripped down.

this.tagInput.keydown(function(event) { // function of the library that checks every keydown event

         // my addition to check for Android keycode input:

                try {
                    var last_key_code_pressed;
                    this.addEventListener('textInput', function(e) {
                        var char = e.data;
                        var kc = char.charCodeAt(0);
                        last_key_code_pressed = kc;
                        console.log(kc); // works to output keycode here but not outside

                        return true;
                    });
                    console.warn(last_key_code_pressed);
                } catch(e) {
                    console.warn("error:" + e);
                }


        // need to get the keycode of the addEventListener HERE


        /* here are if-else-conditions to check if ma, 
        enter or space is pressed (code of the mentioned library) */
Share edited Apr 18, 2017 at 14:51 AlexioVay asked Apr 18, 2017 at 13:49 AlexioVayAlexioVay 4,5374 gold badges35 silver badges54 bronze badges 3
  • 1 it is not possible to get the value outside of this function; although you can pass it to a different function. It's a matter of time; you have no idea when this event will be fired or wether it will ever be fired or how often it will be fired. So your JS won't wait till this happens, because waiting means freezing the whole page (in earlier times, the whole browser). That's what this callback is for, so you get notified as soon as an event happenned, and that you can execute code when this event happened. – Thomas Commented Apr 18, 2017 at 14:15
  • I'm using TagIt (aehlke.github.io/tag-it) and I'm trying to modify the code inside a function this.tagInput.keydown(function(event) { which checks if a ma or space has been pressed to seperate words into tags. I don't know how excatly to pass it to this function? Like I mentioned I just need to make this addition to recognize Android device keycodes. – AlexioVay Commented Apr 18, 2017 at 14:20
  • How do I return the response from an asynchronous call? – Thomas Commented Apr 18, 2017 at 18:56
Add a ment  | 

1 Answer 1

Reset to default 3

This is of course not the only way, but you can assign the keyCode to a variable in the enclosing scope. You could also call a function from the event handler, passing it the keyCode and the storing it.

Example for the first approach:

var input_field = document.getElementById('photo-id');
var last_key_code_pressed;
input_field.addEventListener('textInput', function(e) {
    var char = e.data;
    var keyCode = char.charCodeAt(0);
    last_key_code_pressed = keyCode;

    return true;
});

Edit

a working example (using keydown event instead):

var input_field = document.getElementById('photo-id');
var last_key_code_pressed;
input_field.addEventListener('textInput', function(e) {
    var char = e.data;
    var keyCode = char.charCodeAt(0);
    last_key_code_pressed = keyCode;

    return true;
});

var button = document.getElementById('last-kc');
button.addEventListener('click', function() {
  console.log(last_key_code_pressed);
})
<input id="photo-id" />
<button id="last-kc" type="button">Last KeyCode</button>

发布评论

评论列表(0)

  1. 暂无评论