title: JavaScript Pass variable event to event listener
body: <p>I'm working on a canvas paint project and I'm trying to change the event type of an event listener. For example, a user selects (i) Click or (ii) Mouseover. Then &quot;Click&quot; or &quot;Mouseover&quot; are passed as parameters to the event listener. Been scratching my head on it for a day or so. Not sure if the event is immutable. Anyway thought I'd post it. Also, I'm using range as the input (with values of 0 and 1). Any help would be greatly appreciated.</p>
  <pre><code>&lt;input type=&quot;range&quot; id=&quot;state&quot; name=&quot;state&quot; min=&quot;0&quot; max=&quot;1&quot; value=&quot;0&quot; onchange=&quot;changestate();&quot;&gt;
  </code></pre>
  <pre><code>$state = &quot;mousemove&quot;;  // Global Variable
  
  function changestate(){
  x = document.getElementByID(&quot;state&quot;).value;
  
  if(x == 1){
        $state = &quot;mousemove&quot;;
        } else {
               $state = &quot;click&quot;;
  }
  
  canvas.addEventListener($state, function (evt) {
        //code
  }, false); 
  </code></pre>