<!DOCTYPE html>
<html>
<!--
<?php
// A PHP function
function heb_str($s) // 0 English 1 - Hebrew
{
if (preg_match("/\p{Hebrew}/u", $s) == 1)
return ' dir="rtl"';
else
return "ltr";
}
?>
-->
<head>
<script>
function isheb(str) // false English true Hebrew
{
if ((/[\u0590-\u05FF]/).test(str))
return "rtl";
else
return "ltr";
}
</script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Set Hebrew rtl to a form</title>
</head>
<body>
<form id="mainform" style="width:300px; margin:auto">
<div>Go over ALL the objects in a document in a form (not just input).
Check each of them (INPUT, innerHTML, or TEXTAREA) for Hebrew characters.
If a Hebrew character is found, add a right-to-left(dir="RTL") attribute.</div><hr>
<p class="he" id="p1">abc</p><br>
<p class="he" id="p2">אבג</p><br>
<input id="i1" class="he" type="text" value="abc"><br>
<input id="i2" class="he" type="text" value="אבג"><br><br>
<textarea id="t1" class="he">abc</textarea><br>
<textarea id="t2" class="he">אבג</textarea>
</form>
<script>
function set_heb(form_name)
{
tags = document.getElementById(form_name).elements; // get all the elements in the page
taghe = document.forms["mainform"].getElementsByClassName("he")
for (t=0; t<taghe.length ; t++) {
el = document.getElementById(taghe[t].id);
// console.log(el.tagName); // for further development
if ((el.tagName == "INPUT") || ((el.tagName == "TEXTAREA"))) {
el.dir = isheb(el.value); // get the value content
} else {
el.dir = isheb(el.innerHTML); // get the innerHTML content
}
}
}
set_heb("mainform");
</script>
</body>
</html>