0

I have a code for on click select all text. But unfortunately not working on IE. can anyone give the fix for this.

Need a fix for all browser compatable.

This script is working on below IE 9.0. Not working for IE 10 and above. so kindly give the solution.

Code:

<!DOCTYPE html>
<html>
<head>
<title>autoresizing textarea</title>
<style type="text/css">
#divid {
    display:inline-block;
    border: solid 1px #000;
    min-height: 20px;
    width: 300px;
}
</style>
<script type="text/javascript">
 function selectText(containerid) {
        if (document.selection) {
            var range = document.body.createTextRange();
            range.moveToElementText(document.getElementById(containerid));
            range.select();
        } else if (window.getSelection) {
            var range = document.createRange();
            range.selectNode(document.getElementById(containerid));
            window.getSelection().addRange(range);
        }
    }
</script>

</head>
<body>

<div id="divid" onclick="selectText('divid')">Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>

<div>
                                                    Lorem Ipsum is simply dummy text &nbsp;</div>




</body>
</html>
user3859368
  • 1
  • 1
  • 3

1 Answers1

1

If you only want to select all the text contained in the div onclick...
Just remove all your old script and change your div declaration for this :

<div id="divid" onclick="this.select();">

It's browser compatible.



-----
EDIT

The problem with the script in your question is that it relies on the selection object, which is deprecated since Explorer 11. See here: https://msdn.microsoft.com/en-us/library/ms535869(v=vs.85).aspx

MSDN suggest it be "replaced by" getSelection, but the link to it leads to the method of the deprecated selection object. So...

Anyway, isn't it complicated to just select all?

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64