I used a function that changes the background color according to the user entered a mouse click. In addition, I want that after the user enters a certain color in the input field and presses the Enter key, he should immediately change the background color of the HTML body. Please help me fix this and write the code in pure javascript.
function changeColor(){
var color = document.getElementById("color").value;
document.body.style.background = color;
}
<p>
Enter your fav colour<input type="text" id="color">
<button onclick="changeColor();" onkeypress="changeColor()";> Click </button>
</p>
Run codeHide result
user8094524
source
share