Text deselected on right click in IE8

I want the user selection from a text area in my page. I have a context menu that user can use on right-clicking the selection. This is my code to retrieve selected content from a textarea in IE8,

var textComponent = document.getElementById('myTextArea');
var selectedText;
// IE version
if (document.selection != undefined)
{
textComponent.focus();
selectedText= document.selection.createRange();
alert(sel.text);
}

Now I notice that before allowing the blocked content(i.e the javascript) I can select a text in the text area and on right click it does not get deselected.
But, when I allow the script, on right clicking the text that i selected gets deselected. Which is why i cannot retrieve the content.

  • DOM memory issue with IE8 (inserting lots of JSON data)
  • IE8 and REM based Media Queries
  • How can I prevent a form reset from clearing my <input type=“text”> element?
  • Can't select HTML5 element's children in IE8 with jQuery selector
  • IE8 solution for rounding the corners of slideshow images?
  • Scrolling child div scrolls the page only in IE8
  • I searched the web but didn’t get any solution. Can anyone pls tell me what the problem is?

    Thanks a lot.

  • Result of toJSON() on a date is different between IE8 and IE9+
  • JSON object undefined in Internet Explorer 8
  • Object doesn't support this property or method only on IE8
  • How do I dump JavaScript vars in IE8?
  • What's the best way to add an additional property for each property in an object in IE8
  • IE8: “The system cannot find the path specified” error when acessing window.localStorage
  • One Solution collect form web for “Text deselected on right click in IE8”

    Calling the focus() method of the texbox or using the focus() method of any element on the page will clear the text selection.

    Take a look here for a great answer:

    Keep text selection when focus changes