-1

I have a series of paragraphs and I want to change the background color of some text selected. For example:

<p>The quick brown</p>
<p>fox jumps over</p>
<p>the lazy dog</p>

The user selected the text range from "brown" to "lazy" and clicked on change color button. What I want to do is to do modifications on the selected text such as change color, size etc.

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
  • With "select" you mean mark it with the cursor? – Michael Schmidt Jun 20 '13 at 09:45
  • Yes that's what I mean. – rajeemcariazo Jun 20 '13 at 09:46
  • 1
    Why do you need this? It's going to be rather complicated with getting the selection, inserting spans across several paragraphs etc. – adeneo Jun 20 '13 at 09:47
  • 1
    Take a look to this SO-Question and then change the background with jQuery http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text – Michael Schmidt Jun 20 '13 at 09:47
  • @dTDesign getting the selected text is the easy part. – adeneo Jun 20 '13 at 09:48
  • @adeneo yes i know it's complicated, i would like to get an answer that would at least let me retrieve the nodes under my selection – rajeemcariazo Jun 20 '13 at 09:49
  • @adeneo: so what is the hard part? – Michael Schmidt Jun 20 '13 at 09:50
  • 1
    Maybe will you find some ideas here (http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html) or here (http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text) – Ricola3D Jun 20 '13 at 09:52
  • @dTDesign, Ricola3d, why don't you put those as your answers – rajeemcariazo Jun 20 '13 at 09:54
  • 2
    @dTDesign you must insert a span between the selected text so that you can change the selected text background color. In this case the OP uses more then just one `

    ` so it's going to be very complicated to do this. Imagine if the user selects the _The quick brown fox_, how would you put a `span` in there between two `

    `-s? [Example](http://jsfiddle.net/fSnTX/1)

    – Vucko Jun 20 '13 at 09:55

2 Answers2

1

this is as far as i got (only works for text in the same block element) if anyone can improve on this please do.

replace 'buttonid' with the id for your button

function selectHTML() {

try {
    if (window.ActiveXObject) {
        var c = document.selection.createRange();
        return c.htmlText;
    }

    var nNd = document.createElement("span");
    var w = getSelection().getRangeAt(0);
    w.surroundContents(nNd);
    return nNd.innerHTML;
} catch (e) {
    if (window.ActiveXObject) {
        return document.selection.createRange();
    } else {
        return getSelection();
    }
}

}


$(function() {

$('#button_id').click( function() {
    var mytext = selectHTML();
    $('span').css({"color":"blue"});
});

});
drarkayl
  • 1,017
  • 7
  • 17
0

See Selecting text in an element (akin to highlighting with your mouse)

I hope by the help of this post you will be able to achive what you really want.

Community
  • 1
  • 1
Sanjeev Rai
  • 6,721
  • 4
  • 23
  • 33