Styling text input caret

‘Caret’ is the word you are looking for. I do believe though, that it is part of the browsers design, and not within the grasp of css. However, here is an interesting write up on simulating a caret change using Javascript and CSS http://www.dynamicdrive.com/forums/showthread.php?t=17450 It seems a bit hacky to me, but probably the only … Read more

contenteditable, set caret at the end of the text (cross-browser)

The following function will do it in all major browsers: function placeCaretAtEnd(el) { el.focus(); if (typeof window.getSelection != “undefined” && typeof document.createRange != “undefined”) { var range = document.createRange(); range.selectNodeContents(el); range.collapse(false); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } else if (typeof document.body.createTextRange != “undefined”) { var textRange = document.body.createTextRange(); textRange.moveToElementText(el); textRange.collapse(false); textRange.select(); } } placeCaretAtEnd( … Read more

How to set the caret (cursor) position in a contenteditable element (div)?

In most browsers, you need the Range and Selection objects. You specify each of the selection boundaries as a node and an offset within that node. For example, to set the caret to the fifth character of the second line of text, you’d do the following: function setCaret() { var el = document.getElementById(“editable”) var range … Read more

How to get the caret column (not pixels) position in a textarea, in characters, from the start?

With Firefox, Safari (and other Gecko based browsers) you can easily use textarea.selectionStart, but for IE that doesn’t work, so you will have to do something like this: function getCaret(node) { if (node.selectionStart) { return node.selectionStart; } else if (!document.selection) { return 0; } var c = “\001”, sel = document.selection.createRange(), dul = sel.duplicate(), len … Read more