javascript - Adding an EventListener to button -
i have input[type="text"]
when press down key, adds focus button.
if(e.keycode == 40 && document.getelementsbyclassname('list-item').length > 0){ document.getelementsbyclassname('list-item')[0].focus(); console.log(document.activeelement); }
i want know if it's possible me add eventlistener keyboard events on button, if button input[type="button"]
or <button>
tag. i've added 2 events worked, , 2 events relative keyboard not working. possible?
collection[i].addeventlistener('click', function(e){ console.log('click'); }); collection[i].addeventlistener('focus', function(e){ console.log('focus'); }) collection[i].addeventlistener('keyup', function(e){ console.log('keyup'); }) collection[i].addeventlistener('keypress', function(e){ console.log('keypress'); })
ps: don't want jquery
per documentation, keypress
works keys produce character value. @adeneo mentioned, keyup
seems work on down key. trick focused on button.
https://developer.mozilla.org/en-us/docs/web/events/keypress
Comments
Post a Comment