0

I know that jQuery has .select(); method , the question is can I get the value of this selection ?!

http://api.jquery.com/select/

Simply i need to get the value of Highlighted characters inside text or input box.

Without using any plugins.

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • possible duplicate question. See this question/answer [http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text] (http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text) – Bob Tate Dec 11 '13 at 18:03
  • 1
    possible duplicate of [get highlighted text using jquery .select()?](http://stackoverflow.com/questions/12211964/get-highlighted-text-using-jquery-select) – Horen Dec 11 '13 at 18:03

2 Answers2

1

HTML

<input type="text" name="textbox1" />

jquery

$(document).ready(function(){
    $('input[type="text"]').select(function() {
        alert(window.getSelection());
    });
});
Bryan Elliott
  • 4,055
  • 2
  • 21
  • 22
1

You can use .selectionStart and .selectionEnd. For example: http://jsfiddle.net/tonicboy/225B8/

<input type="text" name="foobar" id="foobar" /><button type="button">Show Value</button>

$("button").click(function() {
    var selected = getSelect($("#foobar"));
    alert('highlighted text: ' + selected);
});

function getSelect($el) { 
    var fullvalue = $el.val(),
        el = $el.get(0);

    return $el.val().substring(el.selectionStart, el.selectionEnd);
}
T Nguyen
  • 3,309
  • 2
  • 31
  • 48