2

For example :

<span id=1>this is my </span><span id=2>selected text</span><span id=3> but this is not selected</span>

I want to catch the list of span id of the text selected by user with jquery.

In this example i want : [1,2]

thanks

pablo07
  • 71
  • 8
  • Question based on supplied code doesn't make sense. You have 3 sibling spans, but you want the IDs of the first two when one of them is selected?? Does not compute.. – Kon May 13 '11 at 16:17

3 Answers3

1

$(window.getSelection().getRangeAt(0).cloneContents()).children("span") will get you the selected spans. The MDC docs are always a good place to start for this kind of this.

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40
  • BTW - you might want to read this thread: http://stackoverflow.com/questions/361130/get-selected-text-and-selected-nodes-on-a-page/364476#364476 – kͩeͣmͮpͥ ͩ May 16 '11 at 09:03
0
var delimitedList = '';
$('pre span').each(function() {
    delimitedList += $(this).attr('id') + ',';
});

//Remove last comma
delimitedList = delimitedList.substring(0, delimitedList.length - 1)
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
0

Maybe you can add a class to the spans that are supposed to be selected, like so -

<span id="1" class="selected">this is my </span><span id="2" class="selected">selected text</span><span id=3> but this is not selected</span>

And then use following jQuery -

var selectedByUser = $("span.selected");
Chantz
  • 5,883
  • 10
  • 56
  • 79