I have been unable to trigger an onselect event handler attached to a <div> element. Is it possible to force a <div> to emit select events?
Onselect event doesn't fire on div
According to w3schools, onSelect is only defined on input and textarea objects.
You may however try to catch the MouseUp event and retrieve the selected text in the current window/document.
You can achieve what you want by using onMouseUp
event. 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>
This is more or less what I'm looking for but it doesn't consider the possibility that the user will use the keyboard to extend or move the selection. But then, binding the same logic to onKeyUp will probably work. –
Phail
Use OnClick
.
:select
only applies to some form elements.
© 2022 - 2024 — McMap. All rights reserved.