Onselect event does not fire on div

I was unable to call the onselect event handler attached to the <div> element . Is it possible to get <div> to emit select events ?

+3
source share
3 answers

According to w3schools , onSelect is defined only for input objects and textarea.

However, you can try to catch the MouseUp event and extract the selected text in the current window / document.

+8
source

You can achieve what you want using the event onMouseUp. See below...

<html>
    <body>
        <div id="container" style="border: 1px solid #333" contentEditable="true" onMouseUp="checkMe()">Type text    here</div>

    </body>
    <script language="javascript">
    function checkMe() {
        var txt = "";

        if (window.getSelection) {
                txt = window.getSelection();
        }
        else if (document.getSelection) {
            txt = document.getSelection();
        } else if (document.selection) {
            txt = document.selection.createRange().text;
        }

        alert("Selected text is " + txt);
    }
    </script>
</html>
+6

OnClick.

:select .

+2

All Articles