LearnFast.Ninja

jQuery which element has focus

Build your custom HTML input elements. Build function to set value for focused element. The main idea is to write input: focus. Test the working example code.

Step 1: Build your custom HTML input elements:

<input type="text" id="Input_Text_focused" />

<br />

<input type="text" value="Not focused" />


Step 2: Build function to set value for focused element :

function Change_Text_On_Selected(Text) {

    $("input:focus").val(Text);

}


Step 3: The main idea:
$("input:focus")

Complete code:

<html>

<head>

    <title>jQuery which element has focus</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            $("#Input_Text_focused").focus();

            Change_Text_On_Selected('Hello focused!');

        });

        function Change_Text_On_Selected(Text) {

            $("input:focus").val(Text);

        }

    </script>

</head>

<body>

    <div>

        This is a tutorial about focused element

    </div>

    <input type="text" id="Input_Text_focused" />

    <br />

    <input type="text" value="Not focused" />

</body>

</html>


Tags

jquery

input focus