1

I have html codes like

<div id="content">
   <span class="pivot">1</span>
   I know this is a really simple
   <span class="pivot">2</span>
   question
   <span class="pivot">3</span>
   to answer 
   <span class="pivot">4</span>
   please 
   <span class="pivot">5</span>
   help me to solve.
</div>

I select the text and I want to get the start_index (this case = 0) and last_index (this case = 39) of this text in the whole text without span of class pivot

Fiddle: http://jsfiddle.net/a7bxp/1/

user247702
  • 23,641
  • 15
  • 110
  • 157
  • Can you trigger the parsing with some button or something else after selecting?
    Or will you always select the first sentence?
    – Alan Piralla Dec 04 '13 at 14:28
  • @AlanPiralla I will trigger by click a button? have a solution? – user3064574 Dec 04 '13 at 14:33
  • @gvee i have looked at this question. And i used this code. It returns start and last, but that isn;t thing i need. Because it includes the text within span class pivot in calculation, while i dont want to count it. – user3064574 Dec 04 '13 at 14:39

1 Answers1

-1

Something like this should give you a go:

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}



$('#your_button').click(function() {
   var whole_text = $('#content').text();
   var selected_text = getSelectionText();
   var index_of = whole_text.indexOf(selected_text);
   var end_of = index_of + selected_text.length;
});

I took the function from here: Get the Highlighted/Selected text

here's a working jFiddle: http://jsfiddle.net/a7bxp/

Community
  • 1
  • 1
Alan Piralla
  • 1,216
  • 2
  • 11
  • 21