Select the text in the same way as "Ctrl + A" when clicking on the text?

I want to select text in a paragraph when I click or double-click a tag <p>. Do not select, just like using the mouse to select a selection area, to select the text you want to select!

I have several links to paragraph links and * .rar on the page, and I want to select all the text when I click on one of them. I think the text box may work this way, but I like that it is in the paragraph or link tag.

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

+5
source share
3 answers

, , :

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);
};
+7
+2

You can select an entire paragraph with a double click. Why do you want to change this?

0
source

All Articles