How to get selected html text with javascript?
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 creating an extra span, but it's a starting point:
var range = window.getSelection().getRangeAt(0),
content = range.extractContents(),
span = document.createElement('SPAN');
span.appendChild(content);
var htmlContent = span.innerHTML;
range.insertNode(span);
alert(htmlContent);
Unfortunately, I can't seem to put the node back as it was (since you can be pulling half the text from a span, for instance).
Retrieve html code of selected text
Do you have a mouse event listener or something before you do contentWindow.getSelection?
If you do you can get the selected node by doing:
function onMouseUp(event) {
var aWindow = event.target.ownerDocument.defaultView;
// should test if aWindow is chrome area or actually content area
var contentWindow = aWindow.document instanceof Ci.nsIHTMLDocument ? aWindow : null; // i guessed here but testing if its content window is done in some similar way
if (!contentWindow) { return }
// do contentWindow.getSelection im not familiar with the code, if selection exists // check if more then one range selected then get node for each, however im going to assume only one range is selected
var nodeOfFirstRange = event.explicitOriginalTarget
var elementOfNode = nodeOfFirstRange.parentNode;
var htmlOfElement = elementOfNode.innerHTML;
}
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.addEventListener('mouseup');
issue with this code is if user mouses down in content window and then highlights and mouseup while mouse its outside of content window, like on chrome window or even outside the browser (like if the browser window was not in maximum or if user mousedup in taskbar of os etc) so just use this code as a guide
get selected text's html in div
Select text and store it in variable called mytext
.
if (!window.x) {
x = {};
}
x.Selector = {};
x.Selector.getSelected = function() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
$(function() {
$(document).bind("mouseup", function() {
var mytext = x.Selector.getSelected();
alert(mytext);
});
});
Check working example at http://jsfiddle.net/YstZn/1/
Get selected option text with JavaScript
Try options
function myNewFunction(sel) { alert(sel.options[sel.selectedIndex].text);}
<select id="box1" onChange="myNewFunction(this);"> <option value="98">dog</option> <option value="7122">cat</option> <option value="142">bird</option></select>
Related Topics
JavaScript Can't Find Element by Id
How to Pass Parameters in Computed Properties in Vue.Js
<Iframe> JavaScript Access Parent Dom Across Domains
How to Stop a Window.Setinterval in JavaScript
How to Set the Prototype of a JavaScript Object That Has Already Been Instantiated
Jquery - Get Text for Element Without Children Text
About Closure, Lexicalenvironment and Gc
Is Onload Equal to Readystate==4 in Xmlhttprequest
Jquery Function to Get All Unique Elements from an Array
Does Console.Log Invokes Tostring Method of an Object
JavaScript Can't Access Private Properties
How to Reset <Input Type = "File">
How to Intercept All Ajax Requests Made by Different Js Libraries
How to Replace Captured Groups Only
Javascript: Simple Way to Check If Variable Is Equal to One of Two or More Values
How to Return a JavaScript String from a Webassembly Function