Skip to content Skip to sidebar Skip to footer

How To Trigger A Keypress Event In Javascript Without JQuery

i have an input id : myinput i want to trigger a keypress event but without any use of jQuery

Solution 1:

You can add event listener to the input for example

var myinput = document.getElementById('myinput');
myinput.addEventListener('keypress',function(){
  //code ...
});

If keypress is not exactly what you looking for there is keydown, keyup, input for diffrent events.


Solution 2:

You can use "onkeypress" HTML event and call a javascript function like "myFunction()".

<input type="text" id="myinput" onkeypress="myFunction()">

<script>
    function myFunction()
    {
     //you code
    }
</script>

Solution 3:

If you want to add "onkeypress" then add event listener. But if you want to trigger event then you could:

var myinput = document.getElementById('myinput');
if (myinput.onkeypress) {
    myinput.onkeypress()
}

Post a Comment for "How To Trigger A Keypress Event In Javascript Without JQuery"