{"id":5332,"date":"2013-05-23T02:00:00","date_gmt":"2013-05-23T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/move-cursor-to-end-input\/"},"modified":"2024-02-08T00:28:46","modified_gmt":"2024-02-07T23:28:46","slug":"move-cursor-to-end-input","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/move-cursor-to-end-input\/","title":{"rendered":"Move cursor to the end of input [JavaScript]"},"content":{"rendered":"<p><strong>You can move the cursor to the end of an input or a textarea by using <code>selectionStart<\/code> and <code>selectionEnd<\/code> properties of input elements using JavaScript.<\/strong><\/p>\n<p>These kind of things might seem simple but can be a bit tricky. So let&#8217;s clear this out and see what&#8217;s the best way of achieving this.<\/p>\n<p>This can be done with both jQuery or JavaScript, but there&#8217;s very little difference between the two so I usually recommend making use of JavaScript.<\/p>\n<h2 class=\"wp-block-heading\" id=\"using-javascript\">Using JavaScript<\/h2>\n<p>This way is very similar to the previous one using jQuery but using <code>sectionEnd<\/code> and <code>sectionStart<\/code> properties of the input.<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">moveCursorToEnd<\/span>(<span class=\"hljs-params\">e<\/span>) <\/span>{\n    setTimeout(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n        <span class=\"hljs-keyword\">const<\/span> input = <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">'#my-input'<\/span>);\n        input.selectionStart = input.selectionEnd = input.value.length;\n        input.focus();\n    }, <span class=\"hljs-number\">0<\/span>)\n}\n\n<span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">'#move'<\/span>).addEventListener(<span class=\"hljs-string\">'click'<\/span>, moveCursorToEnd);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Notice that we are also using <code>setTimeout<\/code>. The reason for it is that Chrome has some bug which causes the focus event to get fired before the cursor is moved into the input.<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_GREaGOm\" data-src=\"\/\/codepen.io\/anon\/embed\/GREaGOm?height=450&amp;theme-id=1&amp;slug-hash=GREaGOm&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed GREaGOm\" title=\"CodePen Embed GREaGOm\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n<h3 class=\"wp-block-heading\" id=\"move-cursor-to-the-end-on-focus\">Move cursor to the end on focus<\/h3>\n<p>Now, what if we want the cursor to move to the end whenever we focus the input?<\/p>\n<p>We will still apply the same function but instead of having a <code>click<\/code> event we will trigger it when the input gets focused.<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">'#my-input'<\/span>).addEventListener(<span class=\"hljs-string\">'focus'<\/span>, moveCursorToEnd);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Here&#8217;s a demo:<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_mdwYKKw\" data-src=\"\/\/codepen.io\/anon\/embed\/mdwYKKw?height=450&amp;theme-id=1&amp;slug-hash=mdwYKKw&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed mdwYKKw\" title=\"CodePen Embed mdwYKKw\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n<p>And if you prefer a more simple solution for these cases, here&#8217;s another approach you can use:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"my-input\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"testvalue\"<\/span> <span class=\"hljs-attr\">onfocus<\/span>=<span class=\"hljs-string\">\"this.value = this.value;\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"test\"<\/span>\/&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2 class=\"wp-block-heading\" id=\"using-jquery\">Using jQuery<\/h2>\n<p>In this case we will be using another approach by just using <code>focus<\/code> and <code>val<\/code>. This is what we&#8217;ll do:<\/p>\n<ul class=\"wp-block-list\">\n<li>\n<ol class=\"wp-block-list\">\n<li>Read the value of the input<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>Remove the input&#8217;s value.<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>Blur the input.<\/li>\n<\/ol>\n<\/li><!-- \/wp:post-content -->\n<!-- wp:list-item --><li>\n<ol start=\"4\">\n<!-- wp:list-item --><li>Focus the input.<\/li><!-- \/wp:list-item -->\n<\/ol><!-- \/wp:list-item -->\n<\/li><!-- \/wp:list-item -->\n<!-- wp:list-item --><li>\n<ol start=\"5\">\n<!-- wp:list-item --><li>Set the value again.<\/li><!-- \/wp:list-item -->\n<\/ol><!-- \/wp:list -->\n<\/li><!-- \/wp:list-item -->\n<\/ul><!-- \/wp:list -->\n<!-- wp:code {\"language\":\"javascript\"} --><pre class=\"wp-block-code\"><code>function moveCursorToEnd(e) {\n    const input = $('#my-input');\n    const originalValue = input.val();\n    input.val('');\n    input.blur().focus().val(originalValue);\n}\n\n$('#move').click(moveCursorToEnd);\n<\/code><\/pre><!-- \/wp:code -->\n<!-- wp:paragraph --><p>Here&#8217;s an example:<\/p><!-- \/wp:paragraph -->\n<!-- wp:cp\/codepen-gutenberg-embed-block {\"penURL\":\"https:\/\/codepen.io\/alvarotrigo\/pen\/MWodXpz\",\"penID\":\"MWodXpz\",\"penType\":\"css,result\"} -->\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_MWodXpz\" data-src=\"\/\/codepen.io\/anon\/embed\/MWodXpz?height=450&amp;theme-id=1&amp;slug-hash=MWodXpz&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed MWodXpz\" title=\"CodePen Embed MWodXpz\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n<!-- \/wp:cp\/codepen-gutenberg-embed-block -->\n<!-- wp:heading --><h2 id=\"related-articles\">Related articles<\/h2><!-- \/wp:heading {\"level\":2} -->\n<!-- wp:list --><ul>\n<!-- wp:list-item --><li><a href=\"https:\/\/alvarotrigo.com\/blog\/wait-1-second-javascript\/\">Wait for 1 second in JavaScript<\/a><\/li><!-- \/wp:list-item -->\n<!-- wp:list-item --><li><a href=\"https:\/\/alvarotrigo.com\/blog\/javascript-alert\/\">JavaScript Alert (Examples and Usage)<\/a><\/li><!-- \/wp:list-item -->\n<!-- wp:list-item --><li><a href=\"https:\/\/alvarotrigo.com\/blog\/javascript-select-option\/\">Change Selected Option in JavaScript (With Examples)<\/a><\/li><!-- \/wp:list-item -->\n<\/ul><!-- \/wp:list -->","protected":false},"excerpt":{"rendered":"<p>You can move the cursor to the end of an input or a textarea by using `selectionStart` and `selectionEnd` properties of input elements using JavaScript.<\/p>\n","protected":false},"author":1,"featured_media":5331,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[107],"tags":[10],"class_list":["post-5332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5332"}],"version-history":[{"count":2,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5332\/revisions"}],"predecessor-version":[{"id":8892,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5332\/revisions\/8892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5331"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}