selectionStart-End with textareas selectionStart-End with textareas javascript javascript

selectionStart-End with textareas


Try:

$('#myarea')[0].selectionStart;

Why? A jQuery selector does not return the actual DOM elements but the wrapped jQuery collection. jQuery makes the actual DOM elements accessible as an array, so if you wanted to use the 1st matched element (and in this case, the only one, since it's by ID), you would do the above.


Since jQuery version 1.6, you can use .prop() method:

Get:

// always start at 0var start = $('#myarea').prop('selectionStart');var end = $('#myarea').prop('selectionEnd');

Set:

$('#myarea').prop('selectionStart', 10);$('#myarea').prop('selectionEnd', 15);// or short hand by$('#myarea').prop({    'selectionStart': 10,    'selectionEnd': 15});