I am writing a Javascript to detect a certain alphabet in a text input by changing a <div>'s color. The code I have written below doesn't seem to be working, how can I solve this problem?
<input onkeypress="myFunction(event)"></input>
<div id="a" style="color:blue">text</div>
<script>
function myFunction(event) {
var x = event.which || event.keyCode;
if (x == 81) {
document.getElementById('a').style.color = red;
} else {
document.getElementById('a').style.color = green;
}
}
</script>
onkeypress, and don't useevent.keyCode. These are deprecated. Look at the big red notices on mdn...