0

This is my sample code. The select function is not working. What I'm doing wrong here. Browser chrome, OS Windows 7.

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:blue; }
  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>
    Click and drag the mouse to select text in the inputs.
  </p>
  <div id="b">Some text in div element</div>


  <div id="a"></div>
<script>
    $(document).ready(function(){
          $("#b").select( function () { 
          $("#a").text("Something was selected").show().fadeOut(1000); 
        });
    });
</script>

</body>
</html>
Abhishek
  • 2,095
  • 2
  • 21
  • 25

3 Answers3

4

jQuery's .select works on <input type="text"> fields and <textarea> boxes, not divs.

John Pick
  • 5,562
  • 31
  • 31
  • Is there any other way to implement this functionality for
    element?
    – Abhishek Mar 02 '12 at 19:01
  • The New York Times seems to be able to do what you need, but you'll have to figure out how it works: http://graphics8.nytimes.com/js/common/screen/altClickToSearch.js – John Pick Mar 02 '12 at 19:06
  • You can have a look at this question http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse – Ronald Mar 02 '12 at 19:08
  • here's an example of how to do this manually: http://motyar.blogspot.de/2010/02/get-user-selected-text-with-jquery-and.html – Tobias Domhan Oct 28 '14 at 07:45
2
<div="a"></div>

should be

<div id="a"></div>
joakimdahlstrom
  • 1,575
  • 1
  • 11
  • 23
0

From the jQuery documentation:

"This event is limited to <input type="text"> fields and <textarea> boxes." http://api.jquery.com/select/

It won't work for a div.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ronald
  • 1,990
  • 6
  • 24
  • 39