getselection

Get start and end of a selection using javascript

眉间皱痕 提交于 2021-02-08 10:14:42
问题 i have an html string like this: <div id="div">Hi how are<span>you?</span> Fine</div> The text will appear "Hi how are you? Fine" . I would like to select a part of the string using mouse and get the beginning and the end of the selection looking from the full text string. For example, if i select "you", i would like to obtain start=11 and end=14; if i select "are you?", i would like to obtain start=7 and end=15. I wrote this to obtain the selection: function getSel() { console.log(navigator

Color change of the selected text in HTML

别来无恙 提交于 2021-02-08 07:34:21
问题 I want to highlight feature which will change the color of the selected text using Javascript. I am using the following method. function android_selection_highlight(replacrmenthtml){ try { if (window.getSelection) { sel = window.getSelection(); var range = sel.getRangeAt(0); var selectionStart = $("<span style=\"color:red\">"); var startRange = document.createRange(); startRange.setStart(range.startContainer, range.startOffset); var selectionEnd = $("</span>"); var endRange = document

How to find index of selected text in getSelection() using javascript?

懵懂的女人 提交于 2021-02-07 03:03:23
问题 I am trying to apply style to the text selected by the user(mouse drag) for which I need to get the start and end index of the selected text. I have tried using "indexOf(...)" method. but it returns the first occurrence of the selected substring. I want the actual position of the substring with respect to the original string. For example.., if I select the letter 'O' at position 3 [YOL O Cobe], I expect the index as 3 but the indexOf() method returns 1 which is the first occurrence of 'O' in

use window.getSelection get selected or cursor located text line from textarea

左心房为你撑大大i 提交于 2020-01-06 03:40:10
问题 I am dynamically filling textarea using Ajax call's. In my code, I want to get the cursor's current line content using window.getSelection. I tried following code, var range = window.getSelection().toString; alert (typeof(range)); But, It returns function as alert Message. or Any other better way, to get the cursor's current line content from textarea that code need to support in all browser's.? Once I get the current line content I will find out line number and update new content on it. 回答1:

getSelection without alt attribute and scripts in it?

◇◆丶佛笑我妖孽 提交于 2020-01-04 04:14:10
问题 I'm using window.getSelection () to get the selected text. But, if i select an image too, it returns also altof an image. EXAMPLE: <img src="someSrc.jpg" alt="image_alt" /> My text here ... if i select an image too, it returns image_alt My text here ... But i need only My text here ... Is there any way to get only text, without alt? Thanks much 回答1: The easiest way would be to use the toString() method of selection's Range(s), which is what window.getSelection().toString() is specified to do

Avoid createElement function if it's inside a <LI> element (contentEditable)

浪子不回头ぞ 提交于 2019-12-30 07:46:47
问题 I'm using this function in order to replace <DIV>New Divs</DIV> with <BR> in break lines on contentEditable divs using Safari and Chrome: $("#form_description").live("keypress", function(e){ if (e.which == 13) { if (window.getSelection) { var selection = window.getSelection(), range = selection.getRangeAt(0), br = document.createElement("br"); range.deleteContents(); range.insertNode(br); range.setStartAfter(br); range.setEndAfter(br); range.collapse(false); selection.removeAllRanges();

window.getSelection() for contenteditable div *on click*

本小妞迷上赌 提交于 2019-12-23 12:55:43
问题 I have a contenteditable div and want to get the user's selection when they click a span . My problem is that when I click the span , the selection gets unselected so window.getSelection().toString() returns '' . How can I get this to work on click of a span ? I know the actual getSelection() works, because if I wrap window.getSelection().toString() in a setTimeout of 5 seconds, after 5 seconds, I get the selected text! My code: $('#btn').click(function() { console.log(window.getSelection()

window.getSelection() for contenteditable div *on click*

你。 提交于 2019-12-23 12:55:02
问题 I have a contenteditable div and want to get the user's selection when they click a span . My problem is that when I click the span , the selection gets unselected so window.getSelection().toString() returns '' . How can I get this to work on click of a span ? I know the actual getSelection() works, because if I wrap window.getSelection().toString() in a setTimeout of 5 seconds, after 5 seconds, I get the selected text! My code: $('#btn').click(function() { console.log(window.getSelection()

How to wrap selected text with a div in ipad?

依然范特西╮ 提交于 2019-12-22 13:56:09
问题 Its a followup question of this. I'm trying to wrap the selected text in a div using window.getSelection() but its not working in iPad because window.getSelection().rangeCount is 0. Demo: http://jsfiddle.net/codef0rmer/jE4w5/ Is there any workaround for this to work in iPad? 回答1: You can use rangy for text selection and highlighting. jQuery Mobile along with backbone.js breaks the page so I used jQuery FastClick plugin 来源: https://stackoverflow.com/questions/11465253/how-to-wrap-selected-text

Google Chart getSelection doesn't have column property

喜欢而已 提交于 2019-12-22 09:17:14
问题 When I use: chart.getChart().getSelection()[0] on a chart (from a chartwrapper, hence the getChart() first), the getSelection() function returns only a row-property but no column property even though my 'chart' is a table and clicking anywhere within it should return both a row and column property. Is this a known google charts bug? Does anyone know of a workaround? Also I have found this topic on google groups: https://groups.google.com/forum/#!topic/google-visualization-api/O_t7-s96A9w here