Using jQuery to highlight the contents of an input field on focus












2















Fiddle:
https://jsfiddle.net/pegarm/aq9Laaew/272358/



I'm creating a utility that's supposed to mimic and work like a spreadsheet consisting of a table of elements. I've created jQuery handlers that support navigation between the "cells" of this table using tab, enter, and arrow keys.



Right now my "proof of concept" code looks like this:



$('input.field').keydown(function(event) {
var
$this = $(this),
Row = getRow($this),
Row_Next = (parseInt(Row) + 1).toString(),
Row_Prev = (parseInt(Row) - 1).toString(),
cursorPosition = $this.getCaret();

switch (event.which) {
/* Enter */
case 13:
event.preventDefault();
if ($this.hasClass('last') && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last')) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Left */
case 37:
if ($this.hasClass('first') && cursorPosition == 0 && Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
} else if (!$this.hasClass('first') && cursorPosition == 0) {
$this.closest('tbody').find('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
}
break;
/* Up */
case 38:
if (Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
/* Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Down */
case 40:
if (Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
}
});


The issue I'm having is that when the user presses tab to navigate between fields, the page automatically selects the content of the next field, allowing them to start typing and overwriting its values. When the user uses arrow keys to navigate between cells, the content is not highlighted forcing them to delete the contents before tying in new values.



Things I've tried which do not work:



$('input.field').focus(function() {
$(this).select();
});


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus().select();


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').select().focus();


...and changing the keydown event handler to:



$('input.field').mouseup(function(e) {
return false;
}).focus(function() {
$(this).select();
}).keydown(function(event) {...


I'm pulling my hair out. Nothing I do seems to allow the contents of the input field to be selected on focus when the arrow keys are used to focus the field.










share|improve this question

























  • Can you create a code snippet showing the issue? Or a fiddle?

    – Darren Sweeney
    Nov 15 '18 at 17:53











  • did you try first to $('input').focus() then $('input').select() ?

    – Nikos M.
    Nov 15 '18 at 17:54











  • @Nikos Yes, with no luck.

    – David Byers
    Nov 15 '18 at 18:12











  • @Darren Fiddle added.

    – David Byers
    Nov 15 '18 at 18:13
















2















Fiddle:
https://jsfiddle.net/pegarm/aq9Laaew/272358/



I'm creating a utility that's supposed to mimic and work like a spreadsheet consisting of a table of elements. I've created jQuery handlers that support navigation between the "cells" of this table using tab, enter, and arrow keys.



Right now my "proof of concept" code looks like this:



$('input.field').keydown(function(event) {
var
$this = $(this),
Row = getRow($this),
Row_Next = (parseInt(Row) + 1).toString(),
Row_Prev = (parseInt(Row) - 1).toString(),
cursorPosition = $this.getCaret();

switch (event.which) {
/* Enter */
case 13:
event.preventDefault();
if ($this.hasClass('last') && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last')) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Left */
case 37:
if ($this.hasClass('first') && cursorPosition == 0 && Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
} else if (!$this.hasClass('first') && cursorPosition == 0) {
$this.closest('tbody').find('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
}
break;
/* Up */
case 38:
if (Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
/* Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Down */
case 40:
if (Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
}
});


The issue I'm having is that when the user presses tab to navigate between fields, the page automatically selects the content of the next field, allowing them to start typing and overwriting its values. When the user uses arrow keys to navigate between cells, the content is not highlighted forcing them to delete the contents before tying in new values.



Things I've tried which do not work:



$('input.field').focus(function() {
$(this).select();
});


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus().select();


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').select().focus();


...and changing the keydown event handler to:



$('input.field').mouseup(function(e) {
return false;
}).focus(function() {
$(this).select();
}).keydown(function(event) {...


I'm pulling my hair out. Nothing I do seems to allow the contents of the input field to be selected on focus when the arrow keys are used to focus the field.










share|improve this question

























  • Can you create a code snippet showing the issue? Or a fiddle?

    – Darren Sweeney
    Nov 15 '18 at 17:53











  • did you try first to $('input').focus() then $('input').select() ?

    – Nikos M.
    Nov 15 '18 at 17:54











  • @Nikos Yes, with no luck.

    – David Byers
    Nov 15 '18 at 18:12











  • @Darren Fiddle added.

    – David Byers
    Nov 15 '18 at 18:13














2












2








2








Fiddle:
https://jsfiddle.net/pegarm/aq9Laaew/272358/



I'm creating a utility that's supposed to mimic and work like a spreadsheet consisting of a table of elements. I've created jQuery handlers that support navigation between the "cells" of this table using tab, enter, and arrow keys.



Right now my "proof of concept" code looks like this:



$('input.field').keydown(function(event) {
var
$this = $(this),
Row = getRow($this),
Row_Next = (parseInt(Row) + 1).toString(),
Row_Prev = (parseInt(Row) - 1).toString(),
cursorPosition = $this.getCaret();

switch (event.which) {
/* Enter */
case 13:
event.preventDefault();
if ($this.hasClass('last') && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last')) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Left */
case 37:
if ($this.hasClass('first') && cursorPosition == 0 && Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
} else if (!$this.hasClass('first') && cursorPosition == 0) {
$this.closest('tbody').find('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
}
break;
/* Up */
case 38:
if (Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
/* Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Down */
case 40:
if (Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
}
});


The issue I'm having is that when the user presses tab to navigate between fields, the page automatically selects the content of the next field, allowing them to start typing and overwriting its values. When the user uses arrow keys to navigate between cells, the content is not highlighted forcing them to delete the contents before tying in new values.



Things I've tried which do not work:



$('input.field').focus(function() {
$(this).select();
});


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus().select();


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').select().focus();


...and changing the keydown event handler to:



$('input.field').mouseup(function(e) {
return false;
}).focus(function() {
$(this).select();
}).keydown(function(event) {...


I'm pulling my hair out. Nothing I do seems to allow the contents of the input field to be selected on focus when the arrow keys are used to focus the field.










share|improve this question
















Fiddle:
https://jsfiddle.net/pegarm/aq9Laaew/272358/



I'm creating a utility that's supposed to mimic and work like a spreadsheet consisting of a table of elements. I've created jQuery handlers that support navigation between the "cells" of this table using tab, enter, and arrow keys.



Right now my "proof of concept" code looks like this:



$('input.field').keydown(function(event) {
var
$this = $(this),
Row = getRow($this),
Row_Next = (parseInt(Row) + 1).toString(),
Row_Prev = (parseInt(Row) - 1).toString(),
cursorPosition = $this.getCaret();

switch (event.which) {
/* Enter */
case 13:
event.preventDefault();
if ($this.hasClass('last') && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last')) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Left */
case 37:
if ($this.hasClass('first') && cursorPosition == 0 && Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
} else if (!$this.hasClass('first') && cursorPosition == 0) {
$this.closest('tbody').find('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-prev') + '"]').focus();
}
break;
/* Up */
case 38:
if (Row != '1') {
$this.closest('tbody').find('tr[data-row="' + Row_Prev + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
/* Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
$this.closest('tr[data-row="' + Row + '"]').find('input[name="' + $this.attr('data-next') + '"]').focus();
}
break;
/* Down */
case 40:
if (Row != '18') {
$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus();
}
break;
}
});


The issue I'm having is that when the user presses tab to navigate between fields, the page automatically selects the content of the next field, allowing them to start typing and overwriting its values. When the user uses arrow keys to navigate between cells, the content is not highlighted forcing them to delete the contents before tying in new values.



Things I've tried which do not work:



$('input.field').focus(function() {
$(this).select();
});


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').focus().select();


...and



$this.closest('tbody').find('tr[data-row="' + Row_Next + '"]').find('input[name="' + $this.attr('name') + '"]').select().focus();


...and changing the keydown event handler to:



$('input.field').mouseup(function(e) {
return false;
}).focus(function() {
$(this).select();
}).keydown(function(event) {...


I'm pulling my hair out. Nothing I do seems to allow the contents of the input field to be selected on focus when the arrow keys are used to focus the field.







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:12







David Byers

















asked Nov 15 '18 at 17:50









David ByersDavid Byers

1162218




1162218













  • Can you create a code snippet showing the issue? Or a fiddle?

    – Darren Sweeney
    Nov 15 '18 at 17:53











  • did you try first to $('input').focus() then $('input').select() ?

    – Nikos M.
    Nov 15 '18 at 17:54











  • @Nikos Yes, with no luck.

    – David Byers
    Nov 15 '18 at 18:12











  • @Darren Fiddle added.

    – David Byers
    Nov 15 '18 at 18:13



















  • Can you create a code snippet showing the issue? Or a fiddle?

    – Darren Sweeney
    Nov 15 '18 at 17:53











  • did you try first to $('input').focus() then $('input').select() ?

    – Nikos M.
    Nov 15 '18 at 17:54











  • @Nikos Yes, with no luck.

    – David Byers
    Nov 15 '18 at 18:12











  • @Darren Fiddle added.

    – David Byers
    Nov 15 '18 at 18:13

















Can you create a code snippet showing the issue? Or a fiddle?

– Darren Sweeney
Nov 15 '18 at 17:53





Can you create a code snippet showing the issue? Or a fiddle?

– Darren Sweeney
Nov 15 '18 at 17:53













did you try first to $('input').focus() then $('input').select() ?

– Nikos M.
Nov 15 '18 at 17:54





did you try first to $('input').focus() then $('input').select() ?

– Nikos M.
Nov 15 '18 at 17:54













@Nikos Yes, with no luck.

– David Byers
Nov 15 '18 at 18:12





@Nikos Yes, with no luck.

– David Byers
Nov 15 '18 at 18:12













@Darren Fiddle added.

– David Byers
Nov 15 '18 at 18:13





@Darren Fiddle added.

– David Byers
Nov 15 '18 at 18:13












1 Answer
1






active

oldest

votes


















2














Seems to be a timing issue (at least in chrome). When I wrap it in a timeout it appears to have the desired results:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
setTimeout(() => {
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
setTimeout(() => {
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
}
break;


Your Fiddle Forked



Update 1 it appears as I had suspected, the browser needs to continue the event in the input where the cursor is. A better solution is to prevent any default from happening:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
event.preventDefault();
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
event.preventDefault();
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}
break;


Second Forked Fiddle






share|improve this answer


























  • Worked like a charm! Thanks Erik!

    – David Byers
    Nov 15 '18 at 22:06











  • No problem, long time no see :P

    – Erik Philips
    Nov 15 '18 at 22:27











  • Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

    – David Byers
    Nov 16 '18 at 23:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325260%2fusing-jquery-to-highlight-the-contents-of-an-input-field-on-focus%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Seems to be a timing issue (at least in chrome). When I wrap it in a timeout it appears to have the desired results:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
setTimeout(() => {
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
setTimeout(() => {
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
}
break;


Your Fiddle Forked



Update 1 it appears as I had suspected, the browser needs to continue the event in the input where the cursor is. A better solution is to prevent any default from happening:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
event.preventDefault();
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
event.preventDefault();
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}
break;


Second Forked Fiddle






share|improve this answer


























  • Worked like a charm! Thanks Erik!

    – David Byers
    Nov 15 '18 at 22:06











  • No problem, long time no see :P

    – Erik Philips
    Nov 15 '18 at 22:27











  • Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

    – David Byers
    Nov 16 '18 at 23:47
















2














Seems to be a timing issue (at least in chrome). When I wrap it in a timeout it appears to have the desired results:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
setTimeout(() => {
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
setTimeout(() => {
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
}
break;


Your Fiddle Forked



Update 1 it appears as I had suspected, the browser needs to continue the event in the input where the cursor is. A better solution is to prevent any default from happening:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
event.preventDefault();
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
event.preventDefault();
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}
break;


Second Forked Fiddle






share|improve this answer


























  • Worked like a charm! Thanks Erik!

    – David Byers
    Nov 15 '18 at 22:06











  • No problem, long time no see :P

    – Erik Philips
    Nov 15 '18 at 22:27











  • Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

    – David Byers
    Nov 16 '18 at 23:47














2












2








2







Seems to be a timing issue (at least in chrome). When I wrap it in a timeout it appears to have the desired results:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
setTimeout(() => {
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
setTimeout(() => {
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
}
break;


Your Fiddle Forked



Update 1 it appears as I had suspected, the browser needs to continue the event in the input where the cursor is. A better solution is to prevent any default from happening:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
event.preventDefault();
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
event.preventDefault();
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}
break;


Second Forked Fiddle






share|improve this answer















Seems to be a timing issue (at least in chrome). When I wrap it in a timeout it appears to have the desired results:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
setTimeout(() => {
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
setTimeout(() => {
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}, 10);
}
break;


Your Fiddle Forked



Update 1 it appears as I had suspected, the browser needs to continue the event in the input where the cursor is. A better solution is to prevent any default from happening:



Right */
case 39:
if ($this.hasClass('last') && cursorPosition == $this.val().length && Row != '18') {
event.preventDefault();
$this.closest('tbody')
.find('tr[data-row="' + Row_Next + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
} else if (!$this.hasClass('last') && cursorPosition == $this.val().length) {
event.preventDefault();
$this.closest('tr[data-row="' + Row + '"]')
.find('input[name="' + $this.attr('data-next') + '"]')
.focus()
.select();
}
break;


Second Forked Fiddle







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 18:30

























answered Nov 15 '18 at 18:22









Erik PhilipsErik Philips

41.3k692126




41.3k692126













  • Worked like a charm! Thanks Erik!

    – David Byers
    Nov 15 '18 at 22:06











  • No problem, long time no see :P

    – Erik Philips
    Nov 15 '18 at 22:27











  • Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

    – David Byers
    Nov 16 '18 at 23:47



















  • Worked like a charm! Thanks Erik!

    – David Byers
    Nov 15 '18 at 22:06











  • No problem, long time no see :P

    – Erik Philips
    Nov 15 '18 at 22:27











  • Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

    – David Byers
    Nov 16 '18 at 23:47

















Worked like a charm! Thanks Erik!

– David Byers
Nov 15 '18 at 22:06





Worked like a charm! Thanks Erik!

– David Byers
Nov 15 '18 at 22:06













No problem, long time no see :P

– Erik Philips
Nov 15 '18 at 22:27





No problem, long time no see :P

– Erik Philips
Nov 15 '18 at 22:27













Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

– David Byers
Nov 16 '18 at 23:47





Indeed. Was wondering if that was the same Erik Philips I knew, but wasn't able to tell due to that stellar profile pic.

– David Byers
Nov 16 '18 at 23:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325260%2fusing-jquery-to-highlight-the-contents-of-an-input-field-on-focus%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine