views:

28

answers:

3

Hi guys :)

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>

Thanks/

+3  A: 

I don't think that's a good idea. A user with mouse in his hand, can click anywhere on the page. If you get him back into textarea, it won't be following the principles of web accessibility.

Sarfraz
A: 

Maybe you mean you want to remember the selection, even if the user focuses on another element, so that if they go back to the textarea, the same text will still be selected. Is this the case?

If so, I think the easiest thing would be to put the textarea in an iframe in the same domain. Each document maintains its own selection context. Of course, you'll need to read the data from the textarea, and probably copy it to a hidden field in your form, as you can't have fields in a form in another document, so you need to make a sort of proxy.

ironfroggy
A: 
           textarea.onblur = function () {
                alert ("Should keep selection");
                textarea.focus();

                return false;
            }
polyhedron
I'm not sure why you want to do this though. But you never know somebody's reasons.
polyhedron