Skip to content
Advertisement

Is it possible to limit text selection to current element?

<div class="container">
<div class="item1">text text text in div 1</div>
<div class="item2">text text text in div 2</div>
</div>

Is it possible (by any HTML node, CSS or JS) to prevent from selecting text in div.item2 if selection started from text in div.item1 and the other way around – starting from div.item2 and limit to it (prevent item1 form being selected)?

Advertisement

Answer

I came up with this, with a bit of jQuery code :

let $items = $(".item")

$items
	.mousedown( function() {
		$(this).siblings().css({ "user-select" : "none" , color : "lightgrey"})	
	})
	.mouseup( function() {
		$items.css({ "user-select" : "auto" , color : "black"})	
	})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
	<div class="item">text text text in div 1</div>
	<div class="item">text text text in div 2</div>
	<div class="item">text text text in div 3</div>
	<div class="item">text text text in div 4</div>
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement