0

I'm looking at the selectors manual for jQuery, here: http://api.jquery.com/category/selectors/. It says, in the beginning, that CSS 1-3 selectors are supported, and then it goes on to list which selectors are supported.

I've been having trouble seeing whether or not ::selection is selectable via jQuery. If it isn't, is there a way to build in support for targeting ::selection via jQuery?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Try it and see if it works? – Barmar Dec 27 '12 at 06:03
  • There's actually an exhaustive list of selectors that jQuery *really* supports in this question: [What CSS3 selectors does jQuery really support, e.g. :nth-last-child()?](http://stackoverflow.com/questions/11745274/what-css3-selectors-does-jquery-really-support-e-g-nth-last-child) In my answer, I state that pseudo-elements are not supported; this means any selector with two colons before its name. – BoltClock Dec 28 '12 at 16:01

1 Answers1

0

Unfortunately, ::selection cannot be used to select element or for that matter the selected text.

But you can get the selected text from the document using plain old JavaScript.

You can see a detailed blog post by Mark Kolich here and a working demo

Here is the code from that post

if(!window.Kolich){
  Kolich = {};
}

Kolich.Selector = {};
Kolich.Selector.getSelected = function(){
  var t = '';
  if(window.getSelection){
    t = window.getSelection();
  }else if(document.getSelection){
    t = document.getSelection();
  }else if(document.selection){
    t = document.selection.createRange().text;
  }
  return t;
}

Kolich.Selector.mouseup = function(){
  var st = Kolich.Selector.getSelected();
  if(st!=''){
    alert("You selected:\n"+st);
  }
}

$(document).ready(function(){
  $(document).bind("mouseup", Kolich.Selector.mouseup);
});
U.P
  • 7,357
  • 7
  • 39
  • 61