Asperger
Asperger

Reputation: 3222

Selection and getSelection. Which object should I use?

There are two objects named selection and getSelection in javascript. Now "Selection" has a lot of properties and methods. For example the method toString, Modify and so on.

The question I am having is which of the two versions should I use, the object selection or getSelection?

No info in caniuse.com

Upvotes: 1

Views: 230

Answers (3)

Thirukumarn.Murugan
Thirukumarn.Murugan

Reputation: 72

Try this .. you may get an idea about  version support and getselection

<head>
    <script type="text/javascript">
        function GetSelectedText() {
            var selText = "";
            if (window.getSelection) {
                //   Supports all browsers, except IE before version 9
                if (document.activeElement &&
                        (document.activeElement.tagName.toLowerCase() == "textarea" ||
                         document.activeElement.tagName.toLowerCase() == "input")) {
                    var text = document.activeElement.value;
                    selText = text.substring(document.activeElement.selectionStart,
                                              document.activeElement.selectionEnd);
                }
                else {
                    var selRange = window.getSelection();
                    selText = selRange.toString();
                }
            }
            else {
                if (document.selection.createRange) { 
// for  Internet Explorer
                    var range = document.selection.createRange();
                    selText = range.text;
                }
            }
            if (selText !== "") {
                alert(selText);
            }
        }
    </script>
</head>
<body onmouseup="GetSelectedText ()">
    Some text for selection.
    <br /><br />
    <textarea>Some text in a textarea element.</textarea>
    <input type="text" value="Some text in an input field." size="40" />
    <br /><br />
    Select some content on this page!
</body>

Upvotes: 0

Thirukumarn.Murugan
Thirukumarn.Murugan

Reputation: 72

Try this--------------------

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    <script>

        function SelectText() {
            var input = document.getElementById("mytextbox");
            input.focus();
            input.setSelectionRange(2, 5);

            var selObj = Window.toString();
            //window.getselection returs the object of current selection
            alert(selObj);

        }
    </script>
</head>
<body>
    <p><input type="text" id="mytextbox" size="20" value="getselection" /></p>
    <p><button onclick="SelectText()">Select text</button></p>
</body>
</html>

Upvotes: 2

mck89
mck89

Reputation: 19241

Those are not the same thing: getSelection returns the current selection on the page as an object that is an instance of Selection. Since the object returned by getSelection is an instance of Selection it will inherit all its methods and properties (including toString, modify and so on). So to anwser your question you must you getSelection to get, set and modify the selection on the page.

Some documentation here on MDN

Upvotes: 1

Related Questions