1

Basically I am trying to get selected text if a user click on a div.

Code:

$('.boldTextButton').mouseup(function(event){
    var SelectionDetails = getSelectionText();
    console.log(SelectionDetails);
});

function getSelectionText() {
    var text = "", startRange=0, endRange=0;
    if (window.getSelection) {
        text = window.getSelection().toString();
        startRange = window.getSelection().anchorOffset;
        endRange = window.getSelection().focusOffset;
    } else if (document.selection && document.selection.type != "Control"){
        text = document.selection.createRange().text;
    }

    return [text, endRange, startRange];
}

Unfortunately with above method, when the mouse is clicked, it first de-select the text and then the returned value I get is empty "".

I also have tried event.preventDefault() and event.stopPropegation(), still didn't work.

4
  • You want to select text when mouse click right? Commented Jul 1, 2020 at 5:16
  • @turivishal, I want to get the selected text when someone click on a div container/button. Commented Jul 1, 2020 at 5:18
  • Do you want to show selected text on a click of a button? Commented Jul 1, 2020 at 5:28
  • does this answer your question stackoverflow.com/a/14553834/8987128 Commented Jul 1, 2020 at 5:34

1 Answer 1

2

Based on my understanding of your question, I guess you want to show selected text on click of a button. So, simply save selection in a hiddenfield and show from there, this will work.

$('#btnSave').click(function(e){
console.log($("#hdnValue").val());
});

$('.boldTextButton').mouseup(function(event){
    var SelectionDetails = getSelectionText();
    $("#hdnValue").val(SelectionDetails);
});

function getSelectionText() {
    var text = "", startRange=0, endRange=0;
    if (window.getSelection) {
        text = window.getSelection().toString();
        startRange = window.getSelection().anchorOffset;
        endRange = window.getSelection().focusOffset;
    } else if (document.selection && document.selection.type != "Control"){
        text = document.selection.createRange().text;
    }

    return [text, endRange, startRange];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="boldTextButton">
<h2>Sample text 1</h2>
<h2>Sample text 2</h2>
<h2>Sample text 3</h2>
<h2>Sample text 4</h2><h2>Sample text 5</h2>
<h2>Sample text 6</h2>
<h2>Sample text 7</h2><h2>Sample text 8</h2>
<h2>Sample text 9</h2>
<h2>Sample text 10</h2><h2>Sample text 11</h2>
</div>

<div class="btnText">
<input type="hidden" id="hdnValue" />
<input type="button" id="btnSave" value="show" />
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

I think above method will require alot of event handlings, such as if user used ctlr+a, shift+arrow and etc for selection and then use the show button for printing, do you know any easier method?
NVM, declared a global variable to store the value, kinda dumb but that only work if you do the following: let SelectionDetails = getSelectionText(); globalSelection = SelectionDetails; and then on show button click, the selection is accessible.
I used hidden field and that should allow getting selected text anytime. the hidden field will work as a global variable and it is a bit more trustable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.