Speaktech.in

jquery get selected text from text input

This jquery script opens a alert box when text is selected inside a text input box in form. This helps to fetch value of selected text and display selected text in another part of website.



   $( ":input" ).select(function() {

 console.log( "Something was selected" );
    
function disp() {
  var text = document.getElementById("train-1-chattext");
  var t = text.value.substr(text.selectionStart, text.selectionEnd - text.selectionStart);
  alert(t);
}

disp();