jQuery.fn.selection  function     

tags: beta

Gets or sets the current text selection.

Getting

Gets the current selection in the context of an element. For example:

$('textarea').selection() // -> { .... }

returns an object with:

  • start - The number of characters from the start of the element to the start of the selection. _ end - The number of characters from teh start of the element to the end of the selection. _ range - A jQuery.Range that represents the current selection.

This lets you do:

var textarea = $('textarea')
  selection = textarea.selection(),
  selected = textarea.val().substr(selection.start, selection.end);

alert('You selected '+selected+'.');

Selection works with all elements. If you want to get selection information on the page:

$(document.body).selection();

Setting

By providing a start and end offset, you can select text within a given element.

$('#rte').selection(30, 40)
$.fn.selection(start, end) -> Object|jQuery
{optional:Number}
  • Start of the range
{optional:Number}
  • End of the range
{Object|jQuery}
  • returns the selection information or the jQuery collection for chaining.
© Jupiter IT - JavaScriptMVC Training and Support