Connecting Tech Pros Worldwide Help | Site Map

Get IDs across Multiple <span> selection

Newbie
 
Join Date: Feb 2007
Posts: 4
#1: Feb 27 '07
I need to have the user select text across many spans (by select, i mean left click and drag) and I want to be able to grab the IDs of all the spans he has selected text from.

I found this article about ranges, and it seems to kinda be what im looking for. I can definitely create a range from the selected text as described there, but if I try to call something like:

myID = range.endContainer.id;

myID is undefined

Any ideas?
Thanks!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Feb 28 '07

re: Get IDs across Multiple <span> selection


What code do you have?
Newbie
 
Join Date: Feb 2007
Posts: 4
#3: Mar 1 '07

re: Get IDs across Multiple <span> selection


Heres what I have so far to detect the users selected text:

Expand|Select|Wrap|Line Numbers
  1. function getSel()
  2. {
  3.     var txt = '';
  4.     var foundIn = '';
  5.     var range;
  6.     if (window.getSelection)
  7.     {        
  8.         txt = window.getSelection();
  9.         range = txt.getRangeAt(0);
  10.         foundIn = 'window.getSelection()';
  11.     }
  12.     else if (document.getSelection)
  13.     {
  14.         txt = document.getSelection();
  15.                 range = txt.getRangeAt(0);
  16.         foundIn = 'document.getSelection()';
  17.     }
  18.     else return 0;
  19.  
  20.     return txt;
  21.  
  22. }
  23.  
  24.  
I'm not quite sure what im doing with 'range' here...ive just been playing with it - no success so far

What I really need is the IDs of the divs/spans I end up pulling the selection from.
Newbie
 
Join Date: Feb 2007
Posts: 4
#4: Mar 1 '07

re: Get IDs across Multiple <span> selection


Ok, I have figured out how to get the ID of one element I am highlighting from:

Expand|Select|Wrap|Line Numbers
  1. unction getSel()
  2. {
  3.     var txt = '';
  4.     var foundIn = '';
  5.     var range;
  6.     if (window.getSelection)
  7.     {        
  8.         txt = window.getSelection();
  9.         alert(txt.anchorNode.parentNode.id);
  10.         foundIn = 'window.getSelection()';
  11.     }
  12.     else if (document.getSelection)
  13.     {
  14.         txt = document.getSelection();
  15.                 alert(txt.anchorNode.parentNode.id);
  16.         foundIn = 'document.getSelection()';
  17.     }
  18.     else return 0;
  19.  
  20.     return txt;
  21.  
  22. }
  23.  
However, when I select across multiple spans I still only get the first spans ID. I want to get the IDs of all the spans I am selecting text from.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Mar 2 '07

re: Get IDs across Multiple <span> selection


This page might help.
Expert
 
Join Date: Nov 2006
Posts: 392
#6: Mar 4 '07

re: Get IDs across Multiple <span> selection


It sounds like what you want to do is have a global event listener for a "mousedown" event, and have your span or dive tags have onmouseover events in them. That way the onmousedown would trigger when you clicked and held the mouse button, and the mouseover events could add their ID values to an array variable, or concatenate them on a String.
Reply