JavaScript dir method returns empty string for text direction

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

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

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

+4
source share
4 answers

I warn during debugging, this has been edited by another user without my consent; the answer that I accepted edited to align it with my question was also overwritten. Since everyone insists on telling me how to code, I have to answer my question so that those who studied on my question can really learn in the style that I am studying. I will leave my +1 vote for the first person, although I will not accept it as an answer, since he did not directly answer my question without excessive changes.

 alert(window.getComputedStyle(document.getElementById('editor_rich')).direction); 
0
source

ltr / rtl is determined by the css "direction" property. It can also be defined using the "dir" attribute for an element in the DOM.

If you are checking the attribute value, use the getAttribute method:

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

Also check the style of the item. By default, it is equal to ltr, therefore, if undefined, should be returned as an empty string. Otherwise, he should say: "rtl"

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

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

+4
source

I would use

 var elm = document.getElementById('example') || document.body; return window.getComputedStyle(elm).direction; 
+2
source

if it is empty, which means that it is not specified in your dir element.

0
source

All Articles