Javascript DOM onkeydown Event via HTML Tag onkeydown function

Introduction

In JavaScript:

object.onkeydown = function(){
       myScript};

This example uses the HTML DOM to assign an "onkeydown" event to an input element.

Press a key inside the text field to set a red background color.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" id="demo">
<script>
document.getElementById("demo").onkeydown = function() {myFunction()};

function myFunction() {/*from   w ww .ja v  a2 s.  co  m*/
  document.getElementById("demo").style.backgroundColor = "red";
}
</script>

</body>
</html>
Bubbles:
Cancelable:
Event type:
Supported HTML tags:












Yes
Yes
KeyboardEvent
All HTML elements,
EXCEPT:
<base>,
<bdo>,
<br>,
<head>,
<html>,
<iframe>,
<meta>,
<param>,
<script>,
<style>, and
<title>



PreviousNext

Related