.focus()

In this chapter you will learn:

  1. Syntax and Description for .focus() event
  2. How to set focus to text box
  3. How to fire focus event
  4. How to set value in case of focus event

Syntax and Description

.focus(handler)           
.focus()

handler is a function to execute each time the event is triggered. Return value is the jQuery object, for chaining purposes.

jQuery focus bind an event handler to the focus JavaScript event, or trigger that event on an element.

.focus(handler) is a shortcut for .bind('focus', handler). .focus() is a shortcut for .trigger('focus').

We can trigger the event when another element is clicked.

$('#other').click(function() {
    $('#target').focus();
});

Set focus to text box

The following code sets focus to text box after the HTML page is loaded.

<html><!--  j  av  a2  s . co m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("input").focus();
        });
    </script>
  </head>
  <body>
    <body>
     <p><input type="text" /> <span>focus event fire</span></p>
    </body>
</html>

Click to view the demo

Fire focus event

The following code fires focus event and adds style to the nearby element.

<html><!-- j  av a2 s  .c o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("input").focus(function () {
                 $(this).next("span").css('color','red');
            });
        });
    </script>
  </head>
  <body>
    <body>
     <p><input type="text" /> <span>focus event fire java2s.com.</span></p>
    </body>
</html>

Click to view the demo

Set value in case of focus event

The following code sets focus form field and sets its value.

<html><!--  j  av a2s .co m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("input").removeAttr("disabled").focus().val("ja va2s.com editable now");
        });
    </script>
  </head>
  <body>
    <body>
      <button>Enable</button>
      <input type="text" disabled="disabled" value="data"/>

  </body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Syntax and Description for .hover() event
  2. How to add hover action to element
  3. How to use hover action to add and remove class
  4. How to use hover action to hide tag
  5. How to use hover to fade in and out
  6. How to use hover event with lists
Home » jQuery » Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()