Re: [jQuery] Select part of text and add a CSS class
On Mon, Jan 18, 2010 at 6:02 AM, Mircea wrote: > I am trying to create my first jQuery script. I want to have a text in > a , select it with the cursor and add a class to it. I know that I > can use the .select and .addClass functions. I bet you need to wrap the selected text in a span - you can't apply a class to an arbitrary block of text, you know? Might not be the only problem with your script but I bet it's at least part of it. :) Good luck, Nathan
[jQuery] Select part of text and add a CSS class
Hi, I am trying to create my first jQuery script. I want to have a text in a , select it with the cursor and add a class to it. I know that I can use the .select and .addClass functions. $("p").live("mouseup", function() { selection = getSelectedText(); if(selection.length >= 3) { $(this).html($(this).html().addClass("selected"); } } ); //Grab selected text function getSelectedText(){ if(window.getSelection){ return window.getSelection().toString(); } else if(document.getSelection){ return document.getSelection(); } else if(document.selection){ return document.selection.createRange().text; } } I can not get this to work. Thank you.