Reputation: 155
I would like to trigger a left button keypress when the user focuses in on an input text field. So far, my code only selects the default value but does not trigger a left button keypress. I actually do need the default value in the text field.
I have:
<label for="name">Name</label>
<input id="name" type="text">
<label for="email">Email</label>
<input id="email" type="email" value="@somemail.com">
I've tried:
$('#email').on('focus', function() {
$(this).trigger($.Event('keypress', {keyCode: 37}));
});
based on JQuery simulating keypress event on an input field but to no avail. Here is a fiddle: https://jsfiddle.net/c9n39r53/
I would like to see this when the form focuses on the email input field:
Upvotes: 0
Views: 229
Reputation: 32
For what is my understanding your plan is to force the placement of the cursor on the begining of the e-mail input.
$('#email').on('focus', function() {
var elem = $(this)
setTimeout(function(){
elem.get(0).setSelectionRange(0,0,0);
},100);
});
Here's my update to your fiddle
Which is a solution like @adeneo proposed. In fact, his is better since it has absolutely no delay.
On your fiddle, you also forgot to include JQuery.
Upvotes: 1