Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call multiple onkeyup events on one html-tag

can i call multiple onkeyup events on the same html input-tag?

<input style="float: left; width: 50px;" id="PersonLength" name="PersonLength" onkeyup="checkNr(this.id)" onkeyup="fill()" type="text"> 

something like that?

<input style="float: left; width: 50px;" id="PersonLength" name="PersonLength"  
onkeyup="checkNr(this.id), fill()" type="text"> 

or like this? ive treid them both but i might have the wrong syntax? or is there a better way to do it?

like image 784
Daniel Gustafsson Avatar asked Nov 29 '22 12:11

Daniel Gustafsson


2 Answers

Another way to Rome:

If you want to call multiple functions and set the event inline, you could write an extra method that will call all methods.

for ex.

<input style="float: left; width: 50px;" id="PersonLength" name="PersonLength" onkeyup="yourElemOnKeyup(this.id)" type="text">

Script

    function yourElemOnKeyup(id){
       checkNr(id);
       fill();
}
like image 39
Mx. Avatar answered Dec 06 '22 03:12

Mx.


You do not call onkeyup events, they are events, they happen.

You can use the EventListener API to add multiple event listeners.

var input = document.getElementById("my_input");
input.addEventListener("keyup", function () {
    doSomething();
});
input.addEventListener("keyup", function () {
    doSomethingElse();
});
// .. etc

The alternative notation would be:

<input .. onkeyup="checkNr(this.id); fill()" type="text">
                                   ^- semicolon

This is similar to:

var input = document.getElementById("my_input");
input.addEventListener("keyup", function () {
    checkNr(input.id);
    fill();
});
like image 70
Halcyon Avatar answered Dec 06 '22 05:12

Halcyon