Search code examples
javascriptdom-events

How to use more than one event handler for a function?


How do I combine more than one Event Handler,

document.getElementById("prix").onchange = function(e)
{
document.getElementById("hhh").innerHTML = this.value*100;
};
document.getElementById("prix").onkeyup = function(e)
{
document.getElementById("hhh").innerHTML = this.value*100;
};

I know this is horrible if I do this, so how can I combine them?


Solution

  • Just define the function and use it twice

    var handler = function (e) { 
      document.getElementById("hhh").innerHTML = this.value*100;
    };
    
    document.getElementById("prix").onchange = handler;
    document.getElementById("prix").onkeyup = handler;