Trigger focus event on a field in jQuery

Description

The following code shows how to trigger focus event on a field.

Example


<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(<!--from w ww .j av a2 s. co  m-->
function() {
$('input').focus(
function() {
$(this).addClass('tmpFocused');
}
);

$('input').blur(
function() {
$(this).removeClass('tmpFocused');
}
);

$('input').trigger('focus');
}
);
</script>
<style type='text/css'>
input.tmpFocused {
background: #5092c5;
color: white;
}
</style>
</head>
<body>
<form method='post' action='javascript:void(0);'>
<div>
<input type='text' name='tmpExample' value='Example' size='25' />
</div>
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Trigger focus event on a field in jQuery
Home »
  Javascript Tutorial »
    jQuery »
      jQuery Form
...
Get value for each form element in jQuery
Handle focus event fired when an element re...
Handle form input Text box selected event i...
Handle form input Text input change event i...
Handle form inut key up event in jQuery
Hide all the input elements within a form i...
Invoke Submit Event in jQuery
Make a form input text element editable and...
Pre-select multiple select option in jQuery
Pre-select options in form select element i...
Preselect checkbox in jQuery
Preset form radio button in jQuery
Select all selected items from form select ...
Select checkbox from a list of form control...
Select checked checkbox in jQuery
Select disabled button and get the its next...
Select form button and check element list l...
Select form file input field in jQuery
Select form image control in jQuery
Select form input text in jQuery
Select form password field in jQuery
Select form radio button in jQuery
Select hidden form fields in jQuery
Select reset button from form in jQuery
Select submit button in jQuery
Set focus to text box in jQuery
Set multiple value selected for select elem...
Set value for form input element in jQuery
Set value for form radio buttons, checkboxe...
Set value for input text element in jQuery
Set value for selected form input in jQuery
Set value to disabled form field in jQuery
Trigger focus event for input element in jQ...
Trigger focus event on a field in jQuery
Turn off submit action in jQuery
Update paragraph text value as you type in ...
Use Click event to replace the button with ...
Use index to get the clicked button index i...
...