I have one scenario where we select the text and a small popup opens for color selection and I have appended this popup at the end of the HTML, now the problem is when I'm going to extend my selection and if that popup is in between that selection the end cursor goes to the end of the HTML because my selection counts the popup HTML, so anyhow can I skip this or avoid for the selection although I'm using user-select property It will avoid the color markup selection but not in the actual selection
Loading

Dr GomathiPosted Nov 6, 2023, 3:25 AM
Hi Abhishek,
To address this, you can use JavaScript to fine-tune the behavior of the text selection. The goal is to detect when the selection overlaps with your popup and then adjust the selection accordingly to exclude the popup from the selected range.
Here's a steps to solve this issue:
Listen for Selection Changes: Set up an event listener for the
mouseupevent, which is triggered when the user finishes making a selection.Determine the Selection's Range: When the selection is changed, use the browser's Selection API to access the current selection range.
Identify Overlap with the Popup: Check if the selection range includes your popup element. You can do this by using methods like
intersectsNodeor comparing the bounding rectangles of the selection range and the popup.Adjust the Selection: If you find that the selection includes the popup, you'll need to create a new selection range that ends just before the popup or starts right after it, depending on the direction of the selection.
Update the Selection: Remove the current selection range and apply the new one that excludes the popup.
Here's an example of how you might write the JavaScript code:
document.addEventListener('mouseup', function(event) {
let selection = window.getSelection();
if (selection.rangeCount > 0) {
let range = selection.getRangeAt(0);
let popupElement = document.getElementById('popup-element-id');
if (popupElement && range.intersectsNode(popupElement)) {
// Create a new range that excludes the popup
let newRange = document.createRange();
newRange.setStart(range.startContainer, range.startOffset);
newRange.setEndBefore(popupElement); // or setStartAfter, depending on your needs
// Clear the selection and apply the new range
selection.removeAllRanges();
selection.addRange(newRange);
}
}
});
This script is a template and might require adjustments to fit the specifics of your application's structure. Also, cross-browser testing is essential to ensure that the behavior is consistent across different environments.
Hope this helps.
Regards,
Dr. Gomathi S