loader image

JavaScript program to change text colour using onfocus() and onblur() events

Change the text colour and background colour of a TextBox using onfocus() and onBlur() event.

<html>

<body>
    Enter text :
    <input type="text" id="txtbox" onfocus=change(0) onblur=change(1)>
    <script>
        function change(i) {
            if (i == 0) {
                document.getElementById("txtbox").style.backgroundColor = "lightgrey";
                document.getElementById("txtbox").style.color = "blue";
            }
            else {
                document.getElementById("txtbox").style.backgroundColor = "white";
                document.getElementById("txtbox").style.color = "black";
            }
        }
    </script>

</body>

</html>
Output
event js 1
event js 2
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
manaal fatima

thank you so much its helpful

Scroll to Top