Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select text just like "Ctrl+A" when clicking the text?

I want to select the text in a paragraph when I click or double click the <p> tag. Not highlight, just like using mouse to make a select area to choose text to be selected!

I have several paragraph and *.rar file link addresses on the page, and I want to select all the text when I click on one of them. I think the textbox could work that way but I like it to be in a paragraph or link tag.

Is there a way to select all text in paragraph by clicking another element?

like image 206
qinHaiXiang Avatar asked Oct 25 '10 02:10

qinHaiXiang


People also ask

How do you select text on a click?

To select a block of text or a paragraph, click at one end of the block. Then, hold down the Shift key and click a second time at the opposite end of the block. You can also select and entire paragraph by triple clicking either the paragraph itself or from the margin by double-clicking the left mouse button.

What is the shortcut key to select a text?

To select a single word, quickly double-click that word. To select a line of text, place your cursor at the start of the line, and press Shift + down arrow. To select a paragraph, place your cursor at the start of the paragraph, and press Ctrl + Shift + down arrow.

What is the meaning of CTRL A?

Ctrl + A Select all contents of the page. Ctrl + B Bold highlighted selection. Ctrl + C Copy selected text. Ctrl + X Cut selected text. Ctrl + P Open the print window.

Why does control a not select all?

the problem “could be” the sticky keys are disabled. Try pressing repetitive and many times the same “Shift” key until a windows pop up with options to turn ON or OFF the Sticky Keys.


1 Answers

Here's a function that will select the contents of the element you pass to it:

function selectElementContents(el) {
    var range;
    if (window.getSelection && document.createRange) {
        range = document.createRange();
        var sel = window.getSelection();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.body && document.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        range.select();
    }
}

window.onload = function() {
    var el = document.getElementById("your_para_id");
    selectElementContents(el);
};
like image 52
Tim Down Avatar answered Oct 05 '22 07:10

Tim Down