JavaScript Focus Events


1. focus: This event occurs when an element gets focus.

Syntax:

    <element onfocus=”functionName()”>


Example:

    <input type=”text” onfocus=”myScript()” id=”input1” >
    <script>
        function myScript()
        {
            document.getElementById(“input1”).style.backgroundColor=”blue”;
        }
    </script>    


2. blur: This event occurs when an element loses focus.

Syntax:

    <element onblur=”functionName()”>


Example:

    <input type=”text” onblur=”myScript()” id=”input1” >
    <script>
        function myScript()
        {
            document.getElementById(“input1”).style.backgroundColor=”yellow”;
        }
    </script>