tags:

views:

38

answers:

3

I need to apply some styles to selected text inside a div, like we can do in MS Word. I could not find any way to get the selected text :(

Thanks

A: 

Take a look at this:

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

xil3
That has limited support and only works on input fields (not <div>)
Marko
A: 

If you just need to style it while it's selected, there's the CSS3 ::selection pseudo-class. This doesn't work in jQuery though.

You
Hey Thanks All ! Actually I want to design an editor similar to tinyMCE and others. But I want the updates done in preview mode, where user can directly see output after he applies some style to the text. Can I have suggestions which one is stable and cross browser compatible ?
KutePHP
@KutePHP - This isn't a small undertaking, that's a *lot* of cross-browser code to write...why not use one of the dozen or so editors that are already out there?
Nick Craver
A: 

Get the jQuery plugin from: http://github.com/localhost/jquery-fieldselection

Then in Your script tag play with it:

$('input[@type="text"], textarea, p').keyup(update).mousedown(update).mousemove(update).mouseup(update);

function update() {
   var range = $(this).getSelection();
   $('#output').html(range.start + range.end + range.length);
}



This links might be helpfoul:

snipplr.com/view/775/getselection/ - Support: IE6, Fx2, Opera9, Safari Cross browser getSelection().

stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea/717226#717226 - How to get selected text in textarea?

stackoverflow.com/questions/2236982/jquery-select-text-and-add-span-to-it-in-an-paragraph

czerasz