I am having a problem in Javascript. In the code below, there are 2 input field with id's "v1" and "v2" respectively. When I try to log the value of input field v1 with only a single character, nothing is shown in the console. After entering the 2nd character, the console shows the first character.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="v1" /><br>
<input type="text" id="v2" />
<script>
document.getElementById("v1").onkeypress = function() {myFunction()};
function myFunction() {
console.log(document.getElementById("v1").value);
document.getElementById("v1").style.backgroundColor = "red";
}
</script>
</body>
</html>
Output: