Search code examples
javascripttextdirection

How to stop Javascript from changing my text input direction?


I'm executing the following JavaScript code:

document.write('ל');
document.write(' 0 1 2');

where the first input is text from a right-to-left language (eg Hebrew or Arabic), and the output is as follows:

ל 0 1 2

with subsequent writes being written right-to-left, until some input is received which is inherently left-to-right (e.g. Latin characters).

The output still appears left-aligned, and setting the css direction attribute to ltr before the second write doesn't fix the problem.

Any ideas where the problem lies?


Solution

  • Use unicode-bidi and direction to style the text:

    <div style="direction:rtl;">ל<span style="direction:ltr;unicode-bidi:bidi-override;">0 1 2</span></div>
    

    The output:

    http://jsfiddle.net/HX4mB/

    Here's a link for more information: http://www.w3.org/TR/CSS2/visuren.html#direction