-1

The problem that I am currently dealing with is that the user has to enter the text in a text area and after the entering of the text the user is free to click on any of the text that he entered, the clicked text than has to be displayed the popup .Following is the code that is not working as per the above problem stated.

<p id="msg">roli</p>
<form onload>
  <center><label>Enter text:</label><br>
    <textarea style="width:100px; height:100px" id="hello" >

    </textarea>
  </center>
</form>
<script>

  var stopCharacters = [' ', '\n', '\r', '\t']
  $(function() {
    document.getElementById("msg").innerHTML="bhanu";
    $('textarea').on('click', function() {
      var text = $(this).html();
      var start = $(this)[0].selectionStart;
      var end = $(this)[0].selectionEnd;
      while (start > 0) {
        if (stopCharacters.indexOf(text[start]) == -1) {
          --start;
        } else {
          break;
        }                        
      };
      ++start;
      while (end < text.length) {
        if (stopCharacters.indexOf(text[end]) == -1) {
          ++end;
        } else {
          break;
        }
      }
      var currentWord = text.substr(start, end - start);
      alert(currentWord);
    });
  });
</script>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
DarkNite
  • 7
  • 4

2 Answers2

0

Did you mean like clicking on the same text box will alert the user with the contents of the text box? If so, you don't need any JavaScript or complex stuff which you have done. Click on the below snippet and click on the text box.

var stopCharacters = [' ', '\n', '\r', '\t'];
$(function() {
  $("#msg").html("bhanu");
  $('textarea').on('click', function() {
    if ($(this).val().trim().length == 0)
      return;
    var textComponent = $(this)[0];
    var currentWord;
    // IE version
    if (document.selection != undefined){
      textComponent.focus();
      var sel = document.selection.createRange();
      currentWord = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined){
      var startPos = textComponent.selectionStart;
      var endPos = textComponent.selectionEnd;
      currentWord = textComponent.value.substring(startPos, endPos)
    }
    alert(currentWord);
  });
});
textarea {display: block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="msg">roli</p>
<form>
  <label for="hello">Enter text:</label>
  <textarea style="width:100px; height:100px" id="hello" ></textarea>
</form>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Here you need to create a range for selection

var stopCharacters = [' ', '\n', '\r', '\t'];
$(function() {
  document.getElementById("msg").innerHTML="bhanu";
  $('textarea').on('click', function() {
    var textComponent = document.getElementById('hello');
    var currentWord;
    // IE version
    if (document.selection != undefined){
      textComponent.focus();
      var sel = document.selection.createRange();
      currentWord = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined){
      var startPos = textComponent.selectionStart;
      var endPos = textComponent.selectionEnd;
      
      while(textComponent.value.charAt(startPos)!=' '){
        if(startPos<=0)break;
        startPos--;
        console.log(startPos);
      }
      while(textComponent.value.charAt(endPos)!=' '){
        if(endPos >= textComponent.value.length)break;
        endPos++;
        console.log(endPos);
      }
      currentWord = textComponent.value.substring(startPos, endPos);
    }
    alert(currentWord);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="msg">roli</p>
<form onload>
  <center><label>Enter text:</label><br>
    <textarea style="width:100px; height:100px" id="hello" ></textarea>
  </center>
</form>
joyBlanks
  • 6,419
  • 1
  • 22
  • 47