-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextboxGetSelectedTextExample01.htm
executable file
·41 lines (36 loc) · 1.45 KB
/
TextboxGetSelectedTextExample01.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
<title>Textbox Get Selected Text Example</title>
<script type="text/javascript" src="EventUtil.js"></script>
</head>
<body>
<p>Select some text in the textbox.</p>
<form method="post" action="javascript:alert('Form submitted!')" id="myForm">
<div>
<label for="comments">Type something:</label><br>
<input type="text" id="txtStuff" name="stuff" value="Default value">
</div>
<input type="submit" value="Submit Form" id="submit-btn">
</form>
<script type="text/javascript">
(function(){
function getSelectedText(textbox){
if (typeof textbox.selectionStart == "number"){
return textbox.value.substring(textbox.selectionStart,
textbox.selectionEnd);
} else if (document.selection){
return document.selection.createRange().text;
}
}
EventUtil.addHandler(window, "load", function(event){
var textbox = document.forms[0].elements[0];
EventUtil.addHandler(textbox, "select", function(event){
alert(getSelectedText(textbox));
});
textbox.focus();
});
})();
</script>
</body>
</html>