Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML of selected text

Tags:

Is there a cross-browser way to get HTML of selected text?

like image 756
Haris Avatar asked Nov 14 '10 09:11

Haris


People also ask

How do you select text in HTML?

HTML | DOM Input Text select() Method The DOM Input select() method selects all the text content of a textarea or an input element which contains the text field. Syntax: element. select();

How do you turn off selected text in HTML?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.

How do you wrap the selected text?

Wrap text around a picture or drawing object Select the picture or object. Select Format and then under Arrange, select Wrap Text. Choose the wrapping option that you want to apply.

How do you highlight a text box in HTML?

Highlight using the HTML5 <mark> tag If you are working on an HTML5 page, the <mark> tag can quickly highlight text. Below is an example of the how to use the mark tag and its result. If your browser supports the <mark> tag, "highlighted text" should have a yellow background.

How to select the first option from the list in HTML?

The <option> tag is used to define the possible options to choose from. The tag is put into <select> tag. The first option from the options’ list is selected by default.

How to get the highlighted/selected text in JavaScript?

How to get the Highlighted/Selected text in JavaScript? There may be a need to find out the text selected/highlighted by the user. It can be done very easily using the window and document objects and their properties. Handling selected text is different for different browsers. The ways to get selected text are shown below: Time to try out the code.

How to get selected text from a document?

It can be done very easily using the window and document objects and their properties. Handling selected text is different for different browsers. The ways to get selected text are shown below: Time to try out the code. Run the code, select a text and press the button to show the selected text:

What is the use of selected attribute in HTML?

HTML selected attribute. selected. The purpose of the HTML selected attribute is to specify whether an option is selected in a form. HTML selected attribute supports option element.


1 Answers

This function will do it in all major browsers:

function getSelectionHtml() {      var html = "";      if (typeof window.getSelection != "undefined") {          var sel = window.getSelection();          if (sel.rangeCount) {              var container = document.createElement("div");              for (var i = 0, len = sel.rangeCount; i < len; ++i) {                  container.appendChild(sel.getRangeAt(i).cloneContents());              }              html = container.innerHTML;          }      } else if (typeof document.selection != "undefined") {          if (document.selection.type == "Text") {              html = document.selection.createRange().htmlText;          }      }      return html;  }      // bind events for selection    document.addEventListener('mouseup', function(){    var selectedHTML = getSelectionHtml();    if( selectedHTML )      console.log( selectedHTML )  });    document.addEventListener('keyup', function(e){     var selectedHTML, key = e.keyCode || e.which;     if( key == 16 ){ // if "shift" key was released      selectedHTML = getSelectionHtml();      if( selectedHTML )        console.log( selectedHTML )    }  });
<ul contenteditable>    <li><p>Select <b>this</b> <em>text</em> right <i>here</i></p></li>    <li>Or <b>this text</b></li>  </ul>
like image 118
Tim Down Avatar answered Sep 23 '22 20:09

Tim Down