getselection

What's the best way to set cursor/caret position?

自作多情 提交于 2019-11-27 10:57:23
If I'm inserting content into a textarea that TinyMCE has co-opted, what's the best way to set the position of the cursor/caret? I'm using tinyMCE.execCommand("mceInsertRawHTML", false, content); to insert the content, and I'd like set the cursor position to the end of the content. Both document.selection and myField.selectionStart won't work for this, and I feel as though this is going to be supported by TinyMCE (through something I can't find on their forum) or it's going to be a really ugly hack. Later: It gets better; I just figured out that, when you load TinyMCE in WordPress, it loads

execCommand insertHTML breaks stored window.getSelection()

江枫思渺然 提交于 2019-11-27 09:52:18
When using methods of selecting text and restoring selected text in a page, I have found that running execCommand('insertHTML... inbetween causes the stored selection to break. This is a sample of how the text is selected and restored. // Get Selection var sel = window.getSelection().getRangeAt(0); // Clear Selections window.getSelection().removeAllRanges(); // Restore Selection window.getSelection().addRange(sel) This works fine, however once you run execCommand('insertHTML.. the selections endOffset sets itself to the same value as the selections startOffset Is there a reason for this? More

getSelection & surroundContents across multiple tags

岁酱吖の 提交于 2019-11-27 07:31:01
I've got a script that changes the background colour of text that has been selected. However i'm encountering an issue when the text is selected across multiple elements/tags. The code that i've got is: var text = window.getSelection().getRangeAt(0); var colour = document.createElement("hlight"); colour.style.backgroundColor = "Yellow"; text.surroundContents(colour); And the error being output is: Error: The boundary-points of a range does not meet specific requirements. = NS_ERROR_DOM_RANGE_BAD_BOUNDARYPOINTS_ERR Line: 7 I believe this is to do with the getRange() function though i'm not too

Need to set cursor position to the end of a contentEditable div, issue with selection and range objects

纵饮孤独 提交于 2019-11-26 21:26:36
问题 I'm forgetting about cross-browser compatibility for the moment, I just want this to work. What I'm doing is trying to modify a script (and you probably don't need to know this) located at typegreek.com The basic script is found here. Basically what it does is when you type in characters, it converts the character your are typing into greek characters and prints it onto the screen. What I'm trying to do is to get it to work on contentEditable div's (It only works for Textareas) My issue is

getSelection & surroundContents across multiple tags

旧时模样 提交于 2019-11-26 13:21:52
问题 I've got a script that changes the background colour of text that has been selected. However i'm encountering an issue when the text is selected across multiple elements/tags. The code that i've got is: var text = window.getSelection().getRangeAt(0); var colour = document.createElement("hlight"); colour.style.backgroundColor = "Yellow"; text.surroundContents(colour); And the error being output is: Error: The boundary-points of a range does not meet specific requirements. = NS_ERROR_DOM_RANGE

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

痞子三分冷 提交于 2019-11-26 10:25:26
I have contentEditable element (can be p, div, ...) and I would like to get caret (cursor) position in it. I can normally achieve it with this piece of code: var position = window.getSelection().getRangeAt(0).startOffset; This works fine while the element contains just text. But when the element contains some HTML formatting, the returned position is relative to caret position within included HTML element. Let's assume contents of contentEditable element is this: AB<b>CD</b>EF If caret is inside <b></b> , let's say between C and D, the returned position with above code is 1 instead of 3

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

这一生的挚爱 提交于 2019-11-26 01:58:28
问题 I have contentEditable element (can be p, div, ...) and I would like to get caret (cursor) position in it. I can normally achieve it with this piece of code: var position = window.getSelection().getRangeAt(0).startOffset; This works fine while the element contains just text. But when the element contains some HTML formatting, the returned position is relative to caret position within included HTML element. Let\'s assume contents of contentEditable element is this: AB<b>CD</b>EF If caret is

How to get selected html text with javascript? [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 00:52:54
问题 This question already has an answer here : HTML of selected text (1 answer) Closed 5 years ago . I can use the following code to get selected text: text=window.getSelection(); /// for Firefox text=document.selection.createRange().text; /// for IE But how can I get the selected Html, which includes the text and html tags? 回答1: In IE <= 10 browsers, it's: document.selection.createRange().htmlText As @DarrenMB pointed out IE11 no longer supports this. See this answer for reference. In non-IE

How to get selected html text with javascript? [duplicate]

℡╲_俬逩灬. 提交于 2019-11-25 19:02:50
This question already has an answer here: HTML of selected text 1 answer I can use the following code to get selected text: text=window.getSelection(); /// for Firefox text=document.selection.createRange().text; /// for IE But how can I get the selected Html, which includes the text and html tags? Mark Kahn In IE <= 10 browsers, it's: document.selection.createRange().htmlText As @DarrenMB pointed out IE11 no longer supports this. See this answer for reference. In non-IE browsers, I just tried playing with this... this seems to work, WILL have side effects from breaking nodes in half and