Get caret (cursor) position in contentEditable area containing HTML content

UPDATE I’ve written a simpler version of this that also works in IE < 9: https://stackoverflow.com/a/4812022/96100 Old Answer This is actually a more useful result than a character offset within the text of the whole document: the startOffset property of a DOM Range (which is what window.getSelection().getRangeAt() returns) is an offset relative to its startContainer … Read more

replace selected text in contenteditable div

The following will do the job in all the major browsers: function replaceSelectedText(replacementText) { var sel, range; if (window.getSelection) { sel = window.getSelection(); if (sel.rangeCount) { range = sel.getRangeAt(0); range.deleteContents(); range.insertNode(document.createTextNode(replacementText)); } } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); range.text = replacementText; } }

How to use [(ngModel)] on div’s contenteditable in angular2?

NgModel expects the bound element to have a value property, which divs don’t have. That’s why you get the No value accessor error. You can set up your own equivalent property and event databinding using the textContent property (instead of value) and the input event: import { Component } from “angular2/core”; @Component({ selector: “my-app”, template: … Read more

Prevent line/paragraph breaks in contentEditable

You can attach an event handler to the keydown or keypress event for the contentEditable field and cancel the event if the keycode identifies itself as enter (or shift+enter). This will disable enter/shift+enter completely when focus is in the contentEditable field. If using jQuery, something like: $(“#idContentEditable”).keypress(function(e){ return e.which != 13; }); …which will return … Read more

Placeholder in contenteditable – focus event issue

Here is a CSS only solution augmenting some of the other answers:- <div contentEditable=true data-ph=”My Placeholder String”></div> <style> [contentEditable=true]:empty:not(:focus)::before{ content:attr(data-ph) } </style> EDIT: Here is my snippet on codepen -> http://codepen.io/mrmoje/pen/lkLez EDIT2: Be advised, this method doesn’t work 100% for multi-line applications due to residual <br> elements being present in the div after performing a … Read more

Insert text at cursor in a content editable div

The following function will insert text at the caret position and delete the existing selection. It works in all the mainstream desktop browsers: function insertTextAtCaret(text) { var sel, range; if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); range.deleteContents(); range.insertNode( document.createTextNode(text) ); } } else if (document.selection && document.selection.createRange) { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)