Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I: Select a paragraph with JavaScript (on click)?

Is it possible to select all the text an an element (e.g., a paragraph <p>) with JavaScript? A lot of people think jQuery .select() would do this, but it does not. It merely triggers an event. Note that DOM objects for <input> elements have a native select() method, but most other elements (such as <output> and <p>) do not.

(Do I need to use content-editable to get this to work?)

like image 910
Alan H. Avatar asked Aug 22 '14 21:08

Alan H.


People also ask

How do you select text in JavaScript?

select() The HTMLInputElement. select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.

How do you select a paragraph in HTML?

The most basic of the selectors is an element selector. For example, paragraphs in an HTML document are represented by the p element and wrapped in <p></p> tags. To select all paragraphs in a document we would use the following selector. To select all level 1 headings we would use the h1 selector.

How do you select this in JavaScript?

To interact with <select> element in JavaScript, you use the HTMLSelectElement type. The HTMLSelectElement type has the following useful properties: selectedIndex – returns the zero-based index of the selected option. The selectedIndex is -1 if no option is selected.

How will you select all paragraph from an HTML page using JS?

getElementsByTagName() that will select all the instances of a certain HTML element on the current webpage based on its tag name, i.e. <div> . Calling document. getElementsByTagName("div") is all you need to do to select all <div> elements on the current page using JavaScript.


2 Answers

If you need to support later browsers, i.e., IE9+, you can use the following

document.querySelector('button').addEventListener('click', function(){
    var range = document.createRange();
    var selection = window.getSelection();
    range.selectNodeContents(document.querySelector('p'));
    
    selection.removeAllRanges();
    selection.addRange(range);
});
                                          
                                          
                                          
                                          
Hello <p>Select me</p> World
<button id ='btn'>Select text</button>

Related links:

  • The spec: http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html#Level2-Range-method-selectNodeContents
  • http://help.dottoro.com/ljcpcpnt.php
  • https://developer.mozilla.org/en-US/docs/Web/API/range.selectNodeContents
  • https://developer.mozilla.org/en-US/docs/Web/API/Selection.addRange

For support across all browsers, see https://github.com/timdown/rangy from https://stackoverflow.com/users/96100/tim-down

like image 126
Juan Mendes Avatar answered Sep 20 '22 03:09

Juan Mendes


select() Will only work on <input> and <textarea> elements...

Also yes, you will have to use:

contenteditable="true"

And use .focus() to select all the text.

Try this:

<p id="editable" contenteditable="true" 
onfocus="document.execCommand('selectAll',false,null);">Your Text Here</p>

<button onclick="document.getElementById('editable').focus();" >Click me</button>

JSFiddle Demo

like image 41
imbondbaby Avatar answered Sep 21 '22 03:09

imbondbaby