Are you first timer at lazacode.org? Why not join us and start exchange your knowledge with everyone. Register today!

JS keypress events

0 votes
5 views
added Feb 14 in Javascript by LC Marshal First Warrant Officer (9,130 points)

HTML type attributes only allows numeric input

<input id="contact" name="" type="tel" maxlength="12"  onkeypress='return event.charCode >= 48 && event.charCode <= 57'>

3 Responses

0 votes
responded Apr 3 by LC Marshal First Warrant Officer (9,130 points)

The event handler can be bound to the input field:

$( "#target" ).keypress(function() {
  console.log( "Handler for .keypress() called." );
});
0 votes
responded Apr 3 by LC Marshal First Warrant Officer (9,130 points)

Javascript - How to assign an onkeypress event to an input element.

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

<script>
function myFunction() {
    document.getElementById("demo").style.backgroundColor = "red";
}
</script>
0 votes
responded Apr 3 by LC Marshal First Warrant Officer (9,130 points)

Uses the addEventListener() method to attach a keypress event to an input element.

<input type="text" id="demo">

<script>
document.getElementById("demo").addEventListener("keypress", myFunction);

function myFunction() {
    document.getElementById("demo").style.backgroundColor = "red";
}
</script>
lazacode.org - Malaysia based Knowledge Exchange Platform, where you can share your finding or solution with other members of community.
...