Speed up development with full-stack environments for every branch.

Learn More

Setting focus to an input field using jQuery [form]

Forked from How to use the jQuery UI Slider.

8988 Runs 78179 Views 17428 Copies


pg1 45

published 4 years ago

<!doctype html>

    <title>Setting focus to an input field using jQuery</title>
            text-align: center;
            font-family: arial;
            font-size: 18px;
            padding: 3px;

            margin:0 auto;
            text-align: left;


    <form method="post" id="form">
        <label>Set focus on page load</label></br>
        <input type="text" name="input1st" />
        <br />
        <br />
        <label>2nd input</label><br />
        <input type="text" name="input2nd" />
        <br />
        <br />
        <input type="button" value="Set focus to 2nd input" /><input type="Submit" />        

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>


        //When DOM loaded we attach click event to button and set focus
        $(document).ready(function() {
            //set focus to 1st input field
            $("#form [name='input1st']").focus();
            //attach click event to button
            $("#form [type='button']").click(function(){
                $("#form [name='input2nd']").focus();



Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).