Can I prevent the loss of selection in the “onblur” event?
JavaScript
x
26
26
1
<!DOCTYPE html>
2
3
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
4
<head>
5
<meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">
6
7
<script type = "text/javascript">
8
window.onload = function () {
9
var textarea = document.getElementsByTagName ("textarea")[0];
10
11
textarea.onblur = function () {
12
alert ("Should keep selection");
13
14
return false;
15
}
16
}
17
</script>
18
19
<title>Select me!</title>
20
</head>
21
22
<body>
23
<textarea>Select me!</textarea>
24
</body>
25
</html>
26
Advertisement
Answer
I don’t think that’s a good idea. A user with mouse in his/her hand can click anywhere on the page. If you get him/her back into the textarea, it won’t be following the principles of web accessibility.