Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect user selection within html element

How can I detect if a user selection (highlighting with mouse) is within/a child of a certain element?

Example:

<div id="parent">
   sdfsdf
   <div id="container">
       some 
      <span>content</span>
   </div>
   sdfsd
</div>

pseudo code:

if window.getSelection().getRangeAt(0) is a child of #container
 return true;
else
 return false;
like image 200
Sam Avatar asked Nov 04 '22 23:11

Sam


1 Answers

Using jQuery on() event handler

$(function() {
     $("#container > * ").on("click", 
         function(event){
            return true;
         });
     });​

Edit: http://jsfiddle.net/9DMaG/1/

<div id="parent">outside
    <div id="container">
        outside
        <span>first_span_clickMe</span>
        <span>second_span_clickMe</span>
    </div>
 outside</div>


$(function() {
   $("#container > span").on("click", function(){
       $('body').append("<br/>child clicked");
   });
});​

like image 123
Eric Fortis Avatar answered Nov 11 '22 03:11

Eric Fortis