2

I have the following page:

<div class="a">
  ...
</div>
<div class="b">
  ...
</div>

My users interact with this page by selecting various pieces of text within .a and .b. The browser's native selection behaviour almost works, but I need to prevent my users from making selections, which span the boundary between .a and .b.

Is there a way to constrain the user's selection to a <div>?

Unfortunately, this content is not user editable - which is unfortunate, because setting contenteditable="true" on each <div> achieves the constraint I'm looking for.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173

1 Answers1

4

How about starting with this:

HTML:

<div class = "a">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris id semper purus. Duis laoreet tellus in ante luctus semper. Praesent interdum urna quis luctus commodo.
</div>
<div class = "b">
    Curabitur vehicula eget leo a tristique. Donec eget aliquam erat. Mauris id porttitor lacus. 
</div>

CSS:

* {
    margin: 0;
    padding: 0;
}

body {
    padding: 10px;
}

.noSelection {
    -webkit-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

div + div {
    padding-top: 10px;
}

jQuery:

$(function() {
    $("div").hover(function() {
        $(this).siblings("div").toggleClass("noSelection");    
    });
});

Here's a fiddle: http://jsfiddle.net/J2fJz/.

DRD
  • 5,557
  • 14
  • 14