Issue
How can I do like, when my cursor leave input field, a function is triggered which change input background color. I was trying to do like this, but did not work well.
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="name" onchange="changeColor()">
<script>
function changeColor() {
const x = document.getElementById("name");
x.style.background = "red";
}
</script>
</body>
</html>
Solution
It seams that you looking for onfocusout event.
function changeColor(ev) {
if (ev.value.length > 0) {
ev.style.background = "red";
}
}
Enter your name: <input type="text" id="name" onfocusout="changeColor(this)">
Answered By - Maik Lowrey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.