Skip to main content
edited title
Link
yantrab
  • 2.7k
  • 4
  • 36
  • 60

Move cursorcaret to the end in contenteditable element and alight text to the end

added jquery to fiddle
Source Link
Nick Parsons
  • 51.7k
  • 6
  • 61
  • 80

In my example i have 2 problems.

1.In the first click, the cursor not moved to the end.

2.In overflow, i need to alight text to the end, because there is free content input.

 $("a").click(function(evt) {   evt.preventDefault();   var textBox = document.querySelector(".c_textBox")   textBox.innerText +="+= " hello a,";   var len = textBox.innerText.length + 1     var range = document.createRange();   var sel = window.getSelection();   range.setStart(textBox.childNodes[0], len );   range.collapse(true);   sel.removeAllRanges();   sel.addRange(range);   textBox.focus();  });
 .c_textBox {   display: block;   width: 200px;   height: 40px;   white-space: nowrap;   overflow: hidden;   font-size: 1.6rem;   border: none;   padding-right: 3rem;   padding-left: 2rem;   background-repeat: no-repeat;   background-size: 2rem;   /* background-position: 100% 50%;*/   background-color: lightgrey;   border: 2px black solid;   padding: 4px;  }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#">add item</a><br/>  <div contenteditable="true" type='text' id="searchInput" class="c_textBox" >class="c_textBox"> </div> 

http://jsfiddle.net/x3pf4nu2/

In my example i have 2 problems.

1.In the first click, the cursor not moved to the end.

2.In overflow, i need to alight text to the end, because there is free content input.

 $("a").click(function(evt){   evt.preventDefault();   var textBox = document.querySelector(".c_textBox")   textBox.innerText +=" hello a,";   var len = textBox.innerText.length + 1     var range = document.createRange();   var sel = window.getSelection();   range.setStart(textBox.childNodes[0], len );   range.collapse(true);   sel.removeAllRanges();   sel.addRange(range);   textBox.focus();  });
 .c_textBox{   display:block;   width:200px;   height:40px;   white-space: nowrap;   overflow: hidden;   font-size: 1.6rem;   border: none;   padding-right: 3rem;   padding-left: 2rem;   background-repeat: no-repeat;   background-size: 2rem;   /* background-position: 100% 50%;*/   background-color:lightgrey;   border: 2px black solid;   padding:4px;  }
  <a href="#">add item</a><br/>  <div contenteditable="true" type='text' id="searchInput" class="c_textBox" > </div> 

http://jsfiddle.net/x3pf4nu2/

In my example i have 2 problems.

1.In the first click, the cursor not moved to the end.

2.In overflow, i need to alight text to the end, because there is free content input.

$("a").click(function(evt) { evt.preventDefault(); var textBox = document.querySelector(".c_textBox") textBox.innerText += " hello a,"; var len = textBox.innerText.length + 1 var range = document.createRange(); var sel = window.getSelection(); range.setStart(textBox.childNodes[0], len); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); textBox.focus(); });
.c_textBox { display: block; width: 200px; height: 40px; white-space: nowrap; overflow: hidden; font-size: 1.6rem; border: none; padding-right: 3rem; padding-left: 2rem; background-repeat: no-repeat; background-size: 2rem; /* background-position: 100% 50%;*/ background-color: lightgrey; border: 2px black solid; padding: 4px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#">add item</a><br/> <div contenteditable="true" type='text' id="searchInput" class="c_textBox"> </div>

http://jsfiddle.net/x3pf4nu2/

snippet added
Source Link
Udhay Titus
  • 5.9k
  • 4
  • 26
  • 43

html:

 <a href="#">add item</a><br/> <div contenteditable="true" type='text' id="searchInput" class="c_textBox" > </div> 

JS:

$("a").click(function(evt){ evt.preventDefault(); var textBox = document.querySelector(".c_textBox") textBox.innerText +=" hello a,"; var len = textBox.innerText.length + 1 var range = document.createRange(); var sel = window.getSelection(); range.setStart(textBox.childNodes[0], len ); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); textBox.focus(); }); 

CSS:

 $("a").click(function(evt){ evt.preventDefault(); var textBox = document.querySelector(".c_textBox") textBox.innerText +=" hello a,"; var len = textBox.innerText.length + 1 var range = document.createRange(); var sel = window.getSelection(); range.setStart(textBox.childNodes[0], len ); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); textBox.focus(); });
 .c_textBox{ display:block; width:200px; height:40px; white-space: nowrap; overflow: hidden; font-size: 1.6rem; border: none; padding-right: 3rem; padding-left: 2rem; background-repeat: no-repeat; background-size: 2rem; /* background-position: 100% 50%;*/ background-color:lightgrey; border: 2px black solid; padding:4px; }
 <a href="#">add item</a><br/> <div contenteditable="true" type='text' id="searchInput" class="c_textBox" > </div> 
.c_textBox{ display:block; width:200px; height:40px; white-space: nowrap; overflow: hidden; font-size: 1.6rem; border: none; padding-right: 3rem; padding-left: 2rem; background-repeat: no-repeat; background-size: 2rem; /* background-position: 100% 50%;*/ background-color:lightgrey; border: 2px black solid; padding:4px; } 
 

html:

 <a href="#">add item</a><br/> <div contenteditable="true" type='text' id="searchInput" class="c_textBox" > </div> 

JS:

$("a").click(function(evt){ evt.preventDefault(); var textBox = document.querySelector(".c_textBox") textBox.innerText +=" hello a,"; var len = textBox.innerText.length + 1 var range = document.createRange(); var sel = window.getSelection(); range.setStart(textBox.childNodes[0], len ); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); textBox.focus(); }); 

CSS:

.c_textBox{ display:block; width:200px; height:40px; white-space: nowrap; overflow: hidden; font-size: 1.6rem; border: none; padding-right: 3rem; padding-left: 2rem; background-repeat: no-repeat; background-size: 2rem; /* background-position: 100% 50%;*/ background-color:lightgrey; border: 2px black solid; padding:4px; } 

 $("a").click(function(evt){ evt.preventDefault(); var textBox = document.querySelector(".c_textBox") textBox.innerText +=" hello a,"; var len = textBox.innerText.length + 1 var range = document.createRange(); var sel = window.getSelection(); range.setStart(textBox.childNodes[0], len ); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); textBox.focus(); });
 .c_textBox{ display:block; width:200px; height:40px; white-space: nowrap; overflow: hidden; font-size: 1.6rem; border: none; padding-right: 3rem; padding-left: 2rem; background-repeat: no-repeat; background-size: 2rem; /* background-position: 100% 50%;*/ background-color:lightgrey; border: 2px black solid; padding:4px; }
 <a href="#">add item</a><br/> <div contenteditable="true" type='text' id="searchInput" class="c_textBox" > </div> 
 

Source Link
yantrab
  • 2.7k
  • 4
  • 36
  • 60
Loading