The onkeyup
and onkeydown
are events triggered by users whenever they try to communicate with a web application. In this shot, we will look at the difference between the onkeyup
and the onkeydown
events. These events are related closely but they are triggered differently.
We use the two event listeners to communicate with our web application. This involves entering information in a particular section of the web application. The event listeners are normally attached directly to an HTML element where the action will be triggered or the HTML element is selected by id
,class
or tag
.
onkeyup
onkeyup()
is used when selecting the tag via id
, class
, or tag
.
onkeyup="function" // is used when attached directly to an HTML tag.
onkeyup
onkeyup
recieves a code snippet or a function to execute.
onkeydown
onkeydown()
is used when selecting the tag via id
, class
, or tag
.
onkeydown="function" // is used when attached directly to an HTML tag.
onkeydown
onkeydown
receives a code snippet or a function to execute.
Let's see a practical example to demonstrate the difference between the two event listeners.
In the code above, we can see the difference between the two event listeners.
"mySelect"
. We also trigger the onkeyup
event listener using onkeyup="myFunction()"
. The passed myFunction()
is used to execute the code. "mySelect2"
. We also trigger the onkeydown
event listener using onkeydown="myFunction2()"
. The passed myFunction2()
is used to execute the code.myFunction
which we called in the onkeyup
event listener.onkeyup
event by its id.myFunction2
which we called in the onkeydown
event listener.onkeydown
event by its id.Observe how everything we enter in the field that is controlled by theonkeyup
event is output completely, but that controlled by theonkeydown
event is missing the last entry. That is whyonkeydown
shouldn't be used for making calculations in realtime.