views:

565

answers:

4

Hi, I have a function that gets the selected text, the text is selected by mouse, and add it into a variable. I want to add tags around that variable in the selected text - in that paragraph.

$("p").live("mouseup",function() {
selection = getSelectedText();
if(selection.length >= 3) {
var $spn = $("<span></span>").html(selection).addClass("selected");
$("p").wrap($spn);
}
});

//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 get the text selection variable but instead of placing the <span></span> around the selected text from the paragraph <p>, my function wraps this outside.

How can I replace it in the paragraph? Thank you.

A: 

This is where it goes wrong:

var $spn = $("<span></span>").html(selection).addClass("selected");
$("p").wrap($spn);

This means you’re wrapping the span around the paragraph.

I think you mean to do something like this:

var spn = '<span class="selected">' + selection + '</span>';
$(this).text($(this).html().replace(selection, spn));
Mathias Bynens
$spn is jQuery, not a string.
tvanfosson
My text gets replaced with [object Object], without <span>. Is there something wrong with my variable?
Mircea
Mircea: check the edit :)
Mathias Bynens
works better, now I get:<span class="selected">selected</span>The span does not parse right, i get > rather then <>
Mircea
I am trying to encode the string but it does not work:$(this).text($(this).html().replace(selection, spn).replace(/
Mircea
Got it!$(this).html($(this).html().replace(selection, spn));I had changed text to html.Thank you all foryour support!
Mircea
A: 

Untested, but should work:

$("p").live("mouseup",function() {
selection = getSelectedText();
if(selection.length >= 3) {
var $spn = $("<span></span>").html(selection).addClass("selected");
$(this).html($(this).html().replace(selection, $spn);
}
});
Daan
$spn is jQuery, not a string.
tvanfosson
I use this:$(document).ready(function(){$("p").live("mouseup",function() {selection = getSelectedText();if(selection.length >= 3) {var $spn = $("<span></span>").html(selection).addClass("selected");$(this).html($(this).html().replace(selection, $spn));}});It seems that I lost my variabile, the selection in paragraph works but It is replaced with [object Object] text - no span
Mircea
Oh, my bad... I just looked into the replacement part, I supposed the rest already worked.
Daan
A: 

I think you'll need to replace the text in the paragraph, not use wrap.

$("p").live("mouseup",function() {
   selection = getSelectedText();
   if(selection.length >= 3) {
      var spn = "<span class='selected'>" + selection + "</span>");
      var html = $(this).html().replace(selection,spn);
      $(this).html(html);
    }
});

Note that this is only going to work reliably if the paragraph contains only text and no markup.

tvanfosson
This wouldn't work if, for example, I selected text accross several tags. If the p is like this : `<p>Some <i>text</i></p>` and I select 'ome tex', your method won't replace anything.
subtenante
@subtenante - I know -- I noted this at the end of the answer, though it could work if the selection algorithm was modified to find the matching section of the DOM and select the entire html (not text). For a simple paragraph containing only text it would work (once).
tvanfosson
Yes, I just made explicit the reason why it wouldn't work. :)
subtenante
A: 

Use .wrapInner() instead of .wrap()

Petr Peller