简体   繁体   中英

Javascript get event from this

I am trying to move from inline onkeyup event and changed my input from

<input name="some1" id="some1" onkeyup="ajax_autocomplete('some1',this.value,event);">

to

<input name="some1" id="some1" class="autoc">

and after page load

var acinputs = document.querySelectorAll('.autoc');
for(var i=0; field=acinputs[i]; i++) 
{
field.onkeyup = function() {update.call(this);}
    function update() 
    {
    var text = this.value;
    // How to get keycode now from event?
    // Before: var kc=event.which;if(kc==undefined){kc=event.keyCode}
    }
}

My problem is that while I was getting event data inline like keycode from the event before but I do not know how to get it from event now? this.value gives me the text entered in the input however this.event does not work! I know this.event is not the proper way of getting event from this !

I am not using jQuery.

Add event as parameter

In my example event is referred to ev .

field.onkeyup = function(ev) {update.call(this, ev);}

function update(th, ev) 
{
    var text = th.value;

    var kc=ev.which;
    if(kc==undefined) {
       kc=ev.keyCode
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM