adding multiple event listeners to one element

后端 未结 14 1471
夕颜
夕颜 2020-11-27 13:12

So my dilemma is that I don\'t want to write the same code twice. Once for the click event and another for the touchstart event.

Here is the original co

14条回答
  •  天涯浪人
    2020-11-27 13:41

    Simplest solution for me was passing the code into a separate function and then calling that function in an event listener, works like a charm.

    function somefunction() { ..code goes here ..}
    
    variable.addEventListener('keyup', function() {
       somefunction(); // calling function on keyup event
    })
    
    variable.addEventListener('keydown', function() {
       somefunction(); //calling function on keydown event
    })
    

提交回复
热议问题