4

This returns an empty string when I try to determine the default text direction...

alert(document.getElementById('example').dir);

I would like to determine if the default text direction is ltr or rtl.

John
  • 1
  • 13
  • 98
  • 177
  • 1
    See http://stackoverflow.com/questions/1169967/inherited-css-values-via-javascript to access inherited style. – Ord Jun 08 '12 at 00:49
  • Do not edit my posts, if you disagree comment first; all edits have only diluted the detail and meaning of my questions. – John Jun 09 '12 at 08:45

4 Answers4

4

ltr/rtl is defined with the css "direction" property. It can also be defined using the "dir" attribute on the element in the DOM.

If you're checking for the attribute value, use the getAttribute method:

 document.getElementById('example').getAttribute('dir');

Also, check the style of the element. It defaults to ltr so if undefined, should return as an empty string. Otherwise it should say "rtl"

 document.getElementById('example').style.direction;

https://developer.mozilla.org/en/CSS/direction

Geuis
  • 41,122
  • 56
  • 157
  • 219
3

I'd use

var elm = document.getElementById('example') || document.body;
return window.getComputedStyle(elm).direction;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

if it is blank that means it isn't specified in your element's dir attribute.

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0

I alert during debugging, like so:

alert(window.getComputedStyle(document.getElementById('editor_rich')).direction);
halfer
  • 19,824
  • 17
  • 99
  • 186
John
  • 1
  • 13
  • 98
  • 177