Set cursor at a length of 14 onfocus of a textbox

IE use different approach at setting cursor position than Firefox,Opera and Chrome. It’s better to make a helper function, which will do it for you. I use this one for own needs. function setCursor(node,pos){ node = (typeof node == “string” || node instanceof String) ? document.getElementById(node) : node; if(!node){ return false; }else if(node.createTextRange){ var textRange … Read more

jquery Setting cursor position in contenteditable div

Maybe I’m misreading the question, but wouldn’t the following do (assuming an editable <div> with id “editable”)? The timer is there because in Chrome, the native browser behaviour that selects the whole element seems to trigger after the focus event, thereby overriding the effect of the selection code unless postponed until after the focus event: … Read more

How to find cursor position in a contenteditable DIV?

If all you want to do is insert some content at the cursor, there’s no need to find its position explicitly. The following function will insert a DOM node (element or text node) at the cursor position in all the mainstream desktop browsers: function insertNodeAtCursor(node) { var range, html; if (window.getSelection && window.getSelection().getRangeAt) { range … Read more

Set the caret/cursor position to the end of the string value WPF textbox

You can set the caret position using CaretIndex property of a TextBox. Please bear in mind that this is not a DependencyProperty. Nevertheless, you may still set it in XAML like this: <TextBox Text=”123″ CaretIndex=”{x:Static System:Int32.MaxValue}” /> Please remember to set CaretIndex after Text property or else it will not work. Thus it probably won’t … Read more

jQuery: Get the cursor position of text in input without browser specific code? [duplicate]

You can’t do this without some browser specific code, since they implement text select ranged slightly differently. However, there are plugins that abstract this away. For exactly what you’re after, there’s the jQuery Caret (jCaret) plugin. For your code to get the position you could do something like this: $(“#myTextInput”).bind(“keydown keypress mousemove”, function() { alert(“Current … Read more

Cursor position in a textarea (character index, not x/y coordinates)

Modified BojanG’s solution to work with jQuery. Tested in Chrome, FF, and IE. (function ($, undefined) { $.fn.getCursorPosition = function() { var el = $(this).get(0); var pos = 0; if(‘selectionStart’ in el) { pos = el.selectionStart; } else if(‘selection’ in document) { el.focus(); var Sel = document.selection.createRange(); var SelLength = document.selection.createRange().text.length; Sel.moveStart(‘character’, -el.value.length); pos = … Read more