-1

How to get the mouse hovered text from the textarea and display the content on a popup window using the javascript.In this the user as the freedom to select and hover any text but the text must be a string only and can't be a number or a special symbol , etc present in the textarea. The code is as follows

Enter text:
                                <br>
                                output:
                                    <span id="out"></span>
                                </div>
                                <script>
                                    function ShowSelectionInsideTextarea()
                                    {
                                        var textComponent = document.getElementById('Words');
                                        var selectedText;
                                        // IE version

                                        if (document.selection != undefined)
                                        {

                                            textComponent.focus();
                                            var sel = document.selection.createRange();
                                            selectedText = sel.text;


                                        }
                                        // Mozilla version
                                        else if (textComponent.selectionStart != undefined)
                                        {
                                            var startPos = textComponent.selectionStart;
                                            var endPos = textComponent.selectionEnd;
                                            selectedText = textComponent.value.substring(startPos, endPos)
                                            window.alert("hello");
                                        }


                                        document.getElementById("out").innerHTML = selectedText;

                                    }

                                    setInterval(ShowSelectionInsideTextarea, 1000);
                                </script>

The problem here is that the alert is not getting the mouse hovered text and is not being displayed.What to do in this case

DarkNite
  • 7
  • 4

1 Answers1

0

Inspired by: How to get selected text from textbox control with javascript

Here you go:

function getSelectionInsideTextarea(textComponent)
{
  var selectedText;

  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  }
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }

  return selectedText;
}

var textArea = document.getElementById('mytextarea');
textArea.onselect = function(){ alert(getSelectionInsideTextarea(textArea)); };

Working example:

https://jsfiddle.net/jve4eehh/6/

Community
  • 1
  • 1
BhavO
  • 2,406
  • 1
  • 11
  • 13