javascriptselectiononblur

Keep the selection in a textarea


Can I prevent the loss of selection in the "onblur" event?

<!DOCTYPE html>

<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">

        <script type = "text/javascript">
            window.onload = function () {
                var textarea = document.getElementsByTagName ("textarea")[0];

                textarea.onblur = function () {
                    alert ("Should keep selection");

                    return false;
                }
            }
        </script>

        <title>Select me!</title>
    </head>

    <body>
        <textarea>Select me!</textarea>
    </body>
</html>

Solution

  • 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.