Change Background Color using onkeydown keyboard event

<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onkeydown="keydownevent()"/> 
<p id="display1">
<p id="display2">
<p id="display3">
<p id="display4">
<p id="display5">


<script>
<!--
function keydownevent()
{
document.getElementById("input1");
                n=document.getElementById("input1").value;
                document.getElementById("display1").style.color="white";  
                switch(Number(n))
                 {
                    case 1: document.getElementById("display1").style.background="pink";  
                    document.getElementById("display1").innerHTML="pink"; 
                     break;
                    case 2: document.getElementById("display2").style.background="blue";
                              document.getElementById("display2").innerHTML="blue"; 
                     break;
                    case 3: document.getElementById("display3").style.background="maroon";
                             document.getElementById("display3").innerHTML="maroon"; 
                     break;
                    case 4:document.getElementById("display4").style.background="green";
                       
                        document.getElementById("display4").innerHTML="green"; 
                     break;
                   default:  document.getElementById("display5").style.background="orange";
                       
                        document.getElementById("display5").innerHTML="orange"; 
                     break;
 
                 
                 }

}
//-->
</script>
</body>
</html>
Output of the Code Above : 
Javascript Events

Enter something here

Comments

Popular posts from this blog

Javascript Code using getElementById and If Else Statement

Javascript Code Using If Else to Compute Discount based on Grades