Skip to content

Instantly share code, notes, and snippets.

@AliMilani
Created February 4, 2022 07:04
Show Gist options
  • Save AliMilani/e9e5e8a1205816350531b846c1546561 to your computer and use it in GitHub Desktop.
Save AliMilani/e9e5e8a1205816350531b846c1546561 to your computer and use it in GitHub Desktop.
input-rtl-direction-fixer
document.addEventListener(
"input",
function (e) {
e = e || window.event;
const target = e.target || e.srcElement;
const isFarsi = /^[\u0600-\u06FF\s]+$/.test(target.value.split(" ")[0]);
const hasDirection = target.style.cssText.includes("direction");
if (
!(target.style.direction == "rtl") &&
isFarsi &&
(!hasDirection || target.style.direction == "ltr")
) {
target.style.direction = "rtl";
console.log("{ set to rtl}");
} else if (
!(target.style.direction == "ltr") &&
!isFarsi &&
(!hasDirection || target.style.direction == "rtl")
) {
console.log("{ set to ltr}");
target.style.direction = "ltr";
}
},
false
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment