views:

50

answers:

2

My JS code:


    function getSelectedText(){
      if(window.getSelection){
          select = window.getSelection().getRangeAt(0);
                  var st_span = select.startContainer.parentNode.getAttribute("id").split("_")[1];
                  var end_span = select.endContainer.parentNode.getAttribute("id").split("_")[1];
                  console.log(select.endContainer);
                  var ret_urn=[st_span,end_span];
                  return ret_urn
      }
      else if(document.getSelection){
          return document.getSelection();
      }

    }
    $(document).ready(function() {
      $("div#check_button button").click(function () {
                      var loc = getSelectedText();
                      console.log(loc);
                      });
    });
    

Here is my whole html file: http://pastebin.com/acdiU623

It is hard to explain it, so I prepared short movie: http://www.youtube.com/watch?v=tVk4K70JO80

In a few words: when I press left mouse button and hold it to select text/numbers and start selection from the half of letter/number, although this letter/number is not highlighted, it is added to selection. I have to start selection precisely. It is ok with wide letters, but hard with letters like i,j or l.

This is second example of my movie. I pressed left button on 3/4 of length of number 5, although 5 is not highlighted, it is selected.

Tested on FF and Opera.

+1  A: 

Ok just tried this demo. and it works flawlessly. it even works on firefox. Just tested opera and safari and it works on both of them as well. Even if i select half a letter or number, it just returns the highlighted text which is what is expected when you make a selection.

try it out on a fresh webpage though just for testing purposes. then when it works and you are satisfied with the results then start making changes to your existing page.

Its a lot more simpler than your code. This is a cross-browser script to get text selected by the user

<script language=javascript>
function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
document.aform.selectedtext.value =  txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()"> 
<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>

http://www.codetoad.com/javascript_get_selected_text.asp

Hope this helps.

PK

Pavan
`window.getSelection()` and `document.getSelection()` both return a `Selection` object, not a string. Make sure you call `toString()` on this object, either explicitly (`txt = window.getSelection().toString()`) or implicitly (`txt = "" + window.getSelection()`).
Tim Down
thats right. you have to call toString() to get the text. so you can do what tim said. thanks a lot for this add on.
Pavan
domi
as i said please try this code separately from your code on a new page. starting from fresh on a clean blank canvas. lol it will help. also in each if statement change it to this instead txt = window.getSelection().toString() and just make sure that txt is a string. rather than document.aform.selectedtext.value = txt;
Pavan
A: 

There are multiple different boundary points for a selection that will look the same to the user. What you're seeing is probably the difference between the following, where | is a selection boundary:

<span>5</span><span>|6</span><span>7|</span><span>8</span>

and

<span>5|</span><span>6</span><span>7</span><span>|8</span>

In both cases, calling toString() on the selection will give you the same result ("67").

Tim Down
It seems that this is the reason of this behavior. Do you know how to handle with this?
domi
What are you actually trying to achieve?
Tim Down
Every character is inside seperate span. Every span has different id. I would like to extract this id from the span which is on the beginning and on the end of selection.I need different ids because I have to identify every span.
domi
OK, but how do you want to "handle" this? The fact is that the selection boundaries just are where they are. Are you looking for the elements that contain the first and last text character in the selection?
Tim Down