Get the direction of the text selection

The following function returns forward if user selected text from the left to right. It returns backward in the other case.

const getDirection = function() {
const selection = window.getSelection();
const range = document.createRange();
range.setStart(selection.anchorNode, selection.anchorOffset);
range.setEnd(selection.focusNode, selection.focusOffset);

return range.collapsed ? 'backward' : 'forward';


Get the default value of a css property
Get the first scrollable parent of an element