Trigger focus event

Description

The following code shows how to trigger focus event.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {<!--  w ww .  jav  a2s.  c  o  m-->
    $("#old").click(function() {
      $("input").trigger("focus");
    });
    $("#new").click(function() {
      $("input").triggerHandler("focus");
    });
    $("input").focus(function() {
      $("<span>Focused!</span>").appendTo("body");
    });
  });
</script>
</head>
<body>
  <button id="old">.trigger("focus")</button>
  <button id="new">.triggerHandler("focus")</button>
  <br />
  <br />
  <input type="text" value="To Be Focused" />
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities