I want to trigger enter key pressed in a text field when a page will be loaded.Suppose when user will load the page a text-field will trigger automatically enter key press event means the text field function should remain same as enter key pressed on keyboard.I have a text filed given below.
<input type="text" id="txt-field" >
Manually when user is pressing enter key form key-board one function is executing which is given below.
function scan(){
alert('hello');
}
But I need this function will execute when page will be load and this text field trigger with enter key event.
I want to trigger enter key pressed in a text field when a page will be loaded.Suppose when user will load the page a text-field will trigger automatically enter key press event means the text field function should remain same as enter key pressed on keyboard.I have a text filed given below.
<input type="text" id="txt-field" >
Manually when user is pressing enter key form key-board one function is executing which is given below.
function scan(){
alert('hello');
}
But I need this function will execute when page will be load and this text field trigger with enter key event.
Share Improve this question asked Apr 21, 2015 at 11:11 rajat_474rajat_474 3481 gold badge4 silver badges15 bronze badges 1- call your function on page load – Tushar Commented Apr 21, 2015 at 11:16
1 Answer
Reset to default 3You can trigger a keypress-event for a specific key when you construct the jQuery Event Object manually. You can assign an event and some properties which will be set on this object.
var e = jQuery.Event( "keydown", { keyCode: 13 } );
$("#txt-field").trigger(e);
In the following line, e
contains the above created event-object with the assigned properties:
$('#txt-field').on('keydown', function (e) {
Demo
Side-Note: There might be some cross-browser issues when the keycodes are different in the browser.
If you only want to trigger the function onload:
$(document).ready(function(){
scan();
});
Reference
jQuery - Event