Q:

Targeting a specific range of text inside a div with jQuery

When a button is pressed, the contents of an editable div:
<div contenteditable="true" id="content">This is some sample text</div>

are sent to my server, which returns a range (like [4,12]) of text that should be highlighted. Is there a way to use jQuery to target that range so I can capture it with a span.

A:

Using Substring function, you can do what you want :
// Your range
var min = 8;
var max = 14;

var text = $.trim($('#content').text());
var text_begin = text.substring(0, min);
var text_highlight = text.substring(min, max);
var text_end = text.substring(max, text.lenght);

$('.result').html(text_begin + '<span style="background: yellow;">'+ text_highlight +'</span>' + text_end);

Here is a fiddle : http://jsfiddle.net/h7dC8/

