How to trigger a keypress in jQuery when the user focuses in on a text field?

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:

Answers


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.


Need Your Help

mergeChangesFromContextDidSaveNotification Consumes Memory

ios core-data

I have two NSManagedObjectContext, one for ui and one for background tasks. I'm trying to merge changes to UIcontext whenever the background one changed. But whenever I call

algorithm to remove elements in the intersection of two sets

c++ algorithm

I have a Visual Studio 2008 C++03 application where I have two standard containers. I would like to remove from one container all of the items that are present in the other container (the intersect...