Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  142] [ 1]  / answers: 1 / hits: 21835  / 11 Years ago, fri, april 26, 2013, 12:00:00

In my project, I am trying to set the caret position always to the end of the text. I know this is default behaviour but when we add some text dynamically, then the caret position changes to starting point in Chrome and firefox (IE is fine, amazing).



Anyway to make it to work properly in chrome and firefox?



Here is the fiddle



<div id=result contenteditable=true></div>
<button class=click>click to add text</butto>





var result = $('#result');
$('.click').click(function () {
var preHtml = result.html();
result.html(preHtml + hello);
result.focus();
});


I tried adding setStart and setEnd as mentioned in this link but no use.


More From » jquery

 Answers
68

I got the solution here thanks to Tim down :). The problem was that I was calling



placeCaretAtEnd($('#result'));


Instead of



placeCaretAtEnd(($('#result').get(0));


as mentioned by jwarzech in the comments.



Working Fiddle


[#78607] Thursday, April 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacie

Total Points: 476
Total Questions: 92
Total Answers: 102

Location: Bosnia and Herzegovina
Member since Tue, Mar 29, 2022
2 Years ago
stacie questions
Fri, Jun 26, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Fri, Aug 2, 19, 00:00, 5 Years ago
;