Skip to content

Instantly share code, notes, and snippets.

@baras
Last active July 27, 2021 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baras/643c22e2d30c20136bde6193e743b610 to your computer and use it in GitHub Desktop.
Save baras/643c22e2d30c20136bde6193e743b610 to your computer and use it in GitHub Desktop.
Set input / textarea direction to rtl if the field contains RTL characters.
$(document).on('input', 'input, textarea', function () {
var $el = $(this),
val = $el.val();
if (val.length) {
$el.css({direction: isRTL(val) ? 'rtl' : 'ltr'});
} else {
$el.css({direction: ""});
}
});
function isRTL(s) {
return (/[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/).test(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment