views:

1159

answers:

3

I have this jQuery toggle. It work fine.

   <ul>
    <li>Go to the store</li>
    <li>Pick up dinner</li>
    <li>Debug crash</li>
    <li>Take a jog</li>
  </ul>

 

        $("li").toggle(
          function () {
            $(this).css({"list-style-type":"disc", "color":"blue"});
          },
          function () {
            $(this).css({"list-style-type":"disc", "color":"red"});
          },
          function () {
            $(this).css({"list-style-type":"", "color":""});
          }
        );

The problem is when I do fast clicking, it highlighted the text in it. Is there a way to stop the text from being highlighted?

+2  A: 
//function to be reused later
function clearSelection() {
  var sel ;
  if(document.selection && document.selection.empty){
    document.selection.empty() ;
  } else if(window.getSelection) {
    sel=window.getSelection();
    if(sel && sel.removeAllRanges)
      sel.removeAllRanges() ;
  }
}

$('p').click(clearSelection);

Source

Alex Bagnolini
Thanks... this did the trick... but ricebowl's suggestion is better... I'll just give you +1.
Reigel
+4  A: 

I'm writing on iPhone, while away from the desk, but a quick Google turned up this page: disable text selection with jQuery.

David Thomas
cool... thanks...
Reigel
A: 
window.getSelection().empty()
Max Shawabkeh
Not cross-browser. In fact, does this work in any browser?
Tim Down
Works on Webkit based browsers at the least.
Max Shawabkeh
Quick test shows it doesn't work in Firefox or IE.
Tim Down