Search Results

Search found 51 results on 3 pages for 'getselection'.

Page 1/3 | 1 2 3  | Next Page >

  • getSelection & surroundContents across multiple tags

    - by lethalbody
    Hi, 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 sure how to proceed since I am a beginner at javascript. Is there any other way I can replicate what I am trying to achieve? Many thanks.

    Read the article

  • getSelection() by Double Click or manual selection is not the same

    - by sanceray3
    Hi all, I allow me to ask a question, because I have a little probleme with an function which returns me the parent of a selection. $('input[type=button].btn_transform').click(function(){ var selectionObj = getSelected();//Function which gives me selection var theParent=selectionObj.anchorNode.parentNode; alert (theParent); }) For example with this sentence : "the cat is <strong>gray</strong>." If I select manually the word "gray" and click on my button, the function returns me [object HTMLSpanElement]. But if I select the same word by double clicking, the function returns me [object HTMLParagraphElement]. Do you know why ? Thanks a lot.

    Read the article

  • Javascript functions return lines of function code or "{[native code]}," what am I doing wrong?

    - by DavidR
    I am writing some code to find the user selection in a contenteditable div, I'm taking my code from this quirksmode article. function findSelection(){ var userSelection; if (window.getSelection) {userSelection = window.getSelection;} else if (document.selection){userSelection = document.selection.createRange();} // For microsoft if (userSelection.text){return userSelection.text} //for Microsoft else {return userSelection} } I'm testing it in Chrome and Firefox, if I do an alert(userSelection) within the function or an alert(findSelection();) outside the function, it returns function getSelection() {[native code]}. If I do console.log(findSelection();) it gives me getSelection(). Is there something I've done wrong?

    Read the article

  • Append before parent after getSelection()

    - by sanceray3
    Hi all, I have one question. I try to develop a little WYSIWYG editor. I would like create a <h1> button which allows me to generate a <h1> title (by clicking on button after text selection in <p> element). I obtain the selection with window.getSelection().... But now, I would like to put my append("<h1>my text selected</h1>") just before the <p>. It’s my problem, because my <p> elements don’t have id or class. So do you know a means to put my append just before the <p> where my text has been selected? I don’t know if with my bad English you’ll understand me! Thanks very much for your help.

    Read the article

  • [JS] getSelection on DIV contentEditable

    - by cactoos
    Hi all ! I am trying to achieve project and i must do a WYSIWYG editor in JavaScript. I can't use an existing editor because I need use my plugins (for example a colorPicker or imagePicker).. For this time I have this HTML : <div class="K_editor" id="idExample"> <div class="K_links"> <div class="K_editor_link K_editor_linkBold">B</div> <div class="K_editor_link K_editor_linkItalic">I</div> <div class="K_editor_link K_editor_linkUnderline">U</div> </div> <iframe width="696" height="212" frameborder="0" src="js/myEditor_iFrame.php"> <html> <head/> <body> <div id="contentIframe" contenteditable="true"> This is a test code, with <strong>bold</strong> text and <em>italic</em> text. </div> </body> </html> </iframe> <input type="submit"/> </div> On event click on ".K_editor_link", a function is open with arguments : - tagStart (exemple '', or '') - tagEnd (exemple '', or '') - id (here 'idExample') I know get a Selection on Textarea but "setSelectionRange()", ".selectionStart" and ".selectionEnd" are only for textbox (XUL), input (XHTML) or textarea (XHTML). Why can I do for do that ?? I'm realy block.. Thank all ! CactO_o's

    Read the article

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

    - by DavidR
    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 with this one function: The user types a key, it get's converted to a greek key, and goes to a function, it gets sorted through some if's, and where it ends up is where I can add div support. Here is what I have so far, myField is the div, myValue is the greek character. //Get selection object... var userSelection if (window.getSelection) {userSelection = window.getSelection();} else if (document.selection) {userSelection = document.selection.createRange();} //Now get the cursor position information... var startPos = userSelection.anchorOffset; var endPos = userSelection.focusOffset; var cursorPos = endPos; //Needed later when reinserting the cursor... var rangeObj = userSelection.getRangeAt(0) var container = rangeObj.startContainer //Now take the content from pos 0 -> cursor, add in myValue, then insert everything after myValue to the end of the line. myField.textContent = myField.textContent.substring(0, startPos) + myValue + myField.textContent.substring(endPos, myField.textContent.length); //Now the issue is, this updates the string, and returns the cursor to the beginning of the div. //so that at the next keypress, the character is inserted into the beginning of the div. //So we need to reinsert the cursor where it was. //Re-evaluate the cursor position, taking into account the added character. var cursorPos = endPos + myValue.length; //Set the caracter position. rangeObj.setStart(container,cursorPos) Now, this works only as long as I don't type more than the size of the original text. Say I had 30 characters in the div before hand. If I type more than that 30, it adds character 31, but places the cursor back at 30. I can type character 32 at pos.31, then character 33 at pos.32, but if I try to put character 34 in, it adds the character, and sets the cursor back at 32. The issue is that the function for adding the new character screws up if cursorPos is greater than what is defined in the range. Any ideas?

    Read the article

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

    - by Daniel Bachhuber
    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 the entire editor in an embedded iframe. Later (2): I can use document.getElementById('content_ifr').contentDocument.getSelection(); to get the selection as a string, but not a Selection Object that I can use getRangeAt(0) on. Making progress little by little.

    Read the article

  • Range Selection and Mozilla

    - by Matrym
    I would like to specify that firefox select a range. I can do this easily with IE, using range.select();. It appears that FFX expects a dom element instead. Am I mistaken, or is there a better way to go about this?

    Read the article

  • Chrome Extension: how to capture selected text and send to a web service

    - by phil swenson
    For the Google Chrome extension, I need to capture selected text in a web page and send to a web service. I'm stuck! First I tried a bookmarklet, but Chrome on Mac seems to have some bookmarklet bugs so I decided to write an extension. I use this code in my ext: function getSelText(){ var txt = 'nothing'; if (window.getSelection){ txt = "1" + window.getSelection(); } else if (document.getSelection) { txt = "2" + document.getSelection(); } else if (document.selection) { txt = "3" + document.selection.createRange().text; } else txt = "wtf"; return txt; } var selection = getSelText(); alert("selection = " + selection); When I click on my extension icon, I get a "1". So I think the act of selecting outside the browser window is causing the text to not be seen by the browser as "selected" any more. Just a theory.... thoughts?

    Read the article

  • Retrieve parent node from selection (range) in Gecko and Webkit

    - by Jason
    I am trying to add an attribute when using a wysiwyg editor that uses "createLink" command. I thought it would be trivial to get back the node that is created after the browse executes that command. Turns out, I am only able to grab this newly created node in IE. Any ideas? The following code demonstrates the issue (debug logs at bottom show different output in each browser): var getSelectedHTML = function() { if ($.browser.msie) { return this.getRange().htmlText; } else { var elem = this.getRange().cloneContents(); return $("<p/>").append($(elem)).html(); } }; var getSelection = function() { if ($.browser.msie) { return this.editor.selection; } else { return this.iframe[0].contentDocument.defaultView.getSelection(); } }; var getRange = function() { var s = this.getSelection(); return (s.getRangeAt) ? s.getRangeAt(0) : s.createRange(); }; var getSelectedNode = function() { var range = this.getRange(); var parent = range.commonAncestorContainer ? range.commonAncestorContainer : range.parentElement ? range.parentElement(): range.item(0); return parent; }; // **** INSIDE SOME EVENT HANDLER **** if ($.browser.msie) { this.ec("createLink", true); } else { this.ec("createLink", false, prompt("Link URL:", "http://")); } var linkNode = $(this.getSelectedNode()); linkNode.attr("rel", "external"); $.log(linkNode.get(0).tagName); // Gecko: "body" // IE: "a" // Webkit: "undefined" $.log(this.getSelectedHTML()); // Gecko: "<a href="http://site.com">foo</a>" // IE: "<A href="http://site.com" rel=external>foo</A>" // Webkit: "foo" $.log(this.getSelection()); // Gecko: "foo" // IE: [object Selection] // Webkit: "foo" Thanks for any help on this, I've scoured related questions on SO with no success!

    Read the article

  • Javascript: Adding selected text to an array

    - by joeybaker
    My goal: each time a user selects text, and clicks a button, that text gets added to an array. The problem: each time the button is pressed, the all objects of the array get overridden with the currently selected text. I'd really appreciate help changing the behavior so that the selected text doesn't override all previous array items. <script type="text/javascript"> var selects = new Array(); selects.push("1"); function getSelText() { var i = 0; while (i<1) { var txt = [null]; var x = 0; if (window.getSelection) { txt[x] = window.getSelection(); } else if (document.getSelection) { txt[x] = document.getSelection(); } else if (document.selection) { txt[x] = document.selection.createRange().text; } else return; selects.push(txt); x++; i++; }; document.menu.selectedtext.value = selects; } </script> <form class="menu" name="menu"> <input type="button" value="highlight" class="highlightButton" onmousedown="getSelText()"/> <textarea name="selectedtext" rows="5" cols="20"></textarea> </form> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

    Read the article

  • How to save highlighting to a database

    - by vman
    Hi I am trying to highlight a part of the text on my website. This highlighted text will be saved for the specific user in a database and when the document is opened again it will show the previously highlighted text. I assumed I would be using javascript to highlight the text but I cannot seem to find a way to pinpoint where the word is that I am highlighting. function getSelText() { var txt = ''; if (window.getSelection) { txt = window.getSelection(); } else if (document.getSelection) { txt = document.getSelection(); } else if (document.selection) { txt = document.selection.createRange().text; } else return ""; return txt; } I am using that to get the selection but I cannot figure out where the selection is in the text. The biggest annoyance is when I have duplicates within the line or text so if I were to use search then I would find the first instance and not the one I was looking for. So the question is : How do you pinpoint a word or a selection in the entire document?

    Read the article

  • contenteditable selected text save and restore

    - by alex
    I came across this post that shows 2 functions on how to save and restore selected text from a contenteditable div. I have the below div set as contenteditable and the 2 function from the other post. How to i use these functions to save and restore selected text. <div style="width:300px;padding:10px;" contenteditable="true">test test test test</div> <script> function saveSelection() { if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { return sel.getRangeAt(0); } } else if (document.selection && document.selection.createRange) { return document.selection.createRange(); } return null; } function restoreSelection(range) { if (range) { if (window.getSelection) { sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } else if (document.selection && range.select) { range.select(); } } } </script>

    Read the article

  • javascript to select text from web page

    - by phil swenson
    I'm trying to write a bookmarklet that grabs any selected text on a web page and sends it to my website. It should (hopefully) work in Chrome, FFX, Safari, and IE. I did a search and found a function, but it doesn't appear to work. Here is the code: <html> <body> <div onClick=getSelText()>Click</div> <div>please select me</div> </body> <script language=javascript> function getSelText(){ var txt = 'nothing'; if (window.getSelection){ txt = "1" + window.getSelection(); } else if (document.getSelection) { txt = "2" + document.getSelection(); } else if (document.selection) { txt = "3" + document.selection.createRange().text; } else return; alert("selected text = " + txt); } </script> </html> when I select the text in the div "please select me" and hit the click div, I just get "selected text = 1" thanks

    Read the article

  • How do I find out the DOM node when select text in a browser’s editable content window using Javascr

    - by chanthou
    I want to get parentElement when I select some text in iframe. iframe = document.getElementById("iframe"); function getParentElement(){ var sel=iframe.contentDocument.getselection(); return sel.parentElement() } getParentElement(); when I run this code it show Error:" TypeError: iframe.contentDocument.getSelection().parentElement is not a function { message="iframe.contentDocument....ement is not a function", more...}" Please give me a help!

    Read the article

  • How to store javascript variables in iPhone application

    - by sugar
    <script type="text/javascript"> function getSelectionRange() { var sel; if (window.getSelection) { sel = window.getSelection(); if (sel.rangeCount) { return sel.getRangeAt(0); } } else if (document.selection) { return document.selection.createRange(); } return null; } var range; </script> whatShouldBeHere=[wvText stringByEvaluatingJavaScriptFromString:@"getSelectionRange();"]; Here, method getSelectionRange is returning JavaScript Object of Range type. How can I catch & store in plist file in iPhone application ? Thanks in advance for sharing your knowledge. Sagar

    Read the article

  • IE9 selectAllChildren on an out-of-view element

    - by MrSlayer
    I am trying to replicate a service that is provided by Tynt.com that appends some text to a user's selection when copying. I understand that users don't particularly like this, but it is a client's request to append the URL and copyright notice whenever a user copies something from their website. In current browsers, I am able to do this by creating a DOM element, adding the selected text, appending the copyright text and then selecting the new node: var newSelection = document.createElement( 'div' ); newSelection.style.cssText = "height: 1px; width: 1px; overflow: hidden;"; if( window.getSelection ) { var selection = window.getSelection( ); if( selection.getRangeAt ) { var range = selection.getRangeAt( 0 ); newSelection.appendChild( range.cloneContents( ) ); appendCopyright( ); document.body.appendChild( newSelection ); selection.selectAllChildren( newSelection ); // ... remove element, return selection } } In IE9, this errors out on the selection.selectAllChildren( newSelection ) statement and I was able to figure out that this is because newSelection was effectively "hidden" from the viewport due to the styles applied in the second line above. Commenting that out works, but obviously the new node is shown to the end user. It appears that this was resolved in later versions of IE, but I am having trouble coming up with a workaround that is sufficient for IE9, a browser that I need to support. I've tried a variety of alternatives, like setting visibility: hidden;, positioning it off-screen, and trying some alternative selection functions, but they each present different problems. The error thrown by IE is: SCRIPT16389: Unspecified error.

    Read the article

  • Problem on "Finding cursor position" function

    - by sanceray3
    Hi all, Few days ago, I have found a solution to obtain the cursor position on a div with contentedit="true". I use the solution defined on this web site : Finding cursor position in a contenteditable div function getCursorPos() { var cursorPos; if (window.getSelection) { var selObj = window.getSelection(); var selRange = selObj.getRangeAt(0); cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset; /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */ } else if (document.selection) { var range = document.selection.createRange(); var bookmark = range.getBookmark(); /* FIXME the following works wrong when the document is longer than 65535 chars */ cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */ } return cursorPos; } function findNode(list, node) { for (var i = 0; i < list.length; i++) { if (list[i] == node) { return i; } } return -1; } It functions well, but when I use html tags inside the div the pointer doesnt show the correct position. For example if I try to find cursor position on the <strong>cat</strong> is black, the function doesn't return me the good position. But if I try on the cat is black, it functions. Any ideas how to get the position with html tags ? Thanks for your help.

    Read the article

  • highlight the text of the DOM range element,

    - by ganapati
    I am able to highlight the text on the HTML page(rendered through gtkmozembed), which is selected, like below. var range, sel; if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt) { range = sel.getRangeAt(0); } document.designMode = "on"; if (range) { sel.removeAllRanges(); sel.addRange(range); } document.execCommand("HiliteColor", false, colour); document.designMode = "off"; } Well,it works very fine.Now i am trying to store the information(startNode, startOffset,endNode, endOffset) about the highlighted text, and next time when i open the same page,highlight the same text. I am able to successfully store the info and retrieve them when the same page opens. And i am trying to highlight the text using following code. var range = document.createRange(); range.setStart(startNode, startOffset); range.setEnd(endNode, endOffset); document.designMode = "on"; range.execCommand("HiliteColor", false, colour); document.designMode = "off"; But it is not working as i am expecting. Can anyone help me to achieve the required? Thanks...

    Read the article

  • Getting the highlighted text inside selected element only.

    - by ashays
    My goal is to be able to get the highlighted text within a document, but only if that text is within a given section, and then apply a certain style to that selected text after clicking a div tag. I'll explain what I mean: So, having looked at window.getSelection() and document.selection.createRange().text, I attempted to use elmnt.getSelection() or elmnt.selection.createRange().text for some HTML element, elmnt. However, it doesn't seem to work, so that idea seems pretty null. This means I can't use this idea to determine the text that is highlighted within a given location. In case this doesn't make sense, essentially, I want html code that looks like this: <body> <div id="content">Stuff here will not be effected</div> <div id="highlightable">Stuff here can be effected when highlighted</div> <div id="morecontent">Stuff here will also not be effected</div> </body> So that whenever I've highlighted text, clicking on a specified div will apply the proper CSS. Now, on to the div tags. Basically, here's what I've got on that: $('.colorpicker').click( function(e) { console.log(getSelectedText()); } Eventually, all I want this to highlight the selected text and have the div tag change the color of the selected text to that of the respective div tag that I've selected. Neither of these seems to be working right now, and my only guess for the reason of the div tag is that it unhighlights whatever I've got selected whenever I click on the div tag. Fallbacks: If there is more than one time that 'abc' is found on the page and I highlight to color 'abc', I would like that only that copy of 'abc' be highlighted. I know this is a lot in one question, but even if I could get a little head start on this idea, my next personal project would be going a lot more smoothly. Thanks. :)

    Read the article

  • Javascript: I can get the text of a selection, now how do I get the text outside the selection?

    - by DavidR
    I have a bit of code that returns the text of a selection and I can assign that string to a variable, but now all I need are two variables, one for the text before the selection and one for the text after the selection. Here is the code for getting the selection: function findSelection(){ //returns the selection object. var userSelection; if (window.getSelection) {userSelection = window.getSelection();} // Mozilla Selection object. else if (document.selection){userSelection = document.selection.createRange();} //gets Microsoft Text Range, should be second b/c Opera has poor support for it. if (userSelection.text){return userSelection.text} //for Microsoft Objects. else {return userSelection} //For Mozilla Objects. } Then I find the anchorOffset and focusOffset to find the caret positions. I tried using these to modify a range object, like this: var range = document.createRange(); range.setStart(textdiv,0); range.setEnd(textdiv,5); Where textdiv is a variable holding the last div the user clicked on. The problem is firefox just gives me a "Security error" code: "1000" at the line range.setStart(textdiv,0);. Is there an easier way to go about doing this? I really just need the text and nothing more.

    Read the article

1 2 3  | Next Page >