Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Element.dir

« Element

The HTMLElement.dir attribute gets or sets the text writing directionality of the content of the current element.

The text writing directionality of an element is which direction that text goes (for support of different language systems). Arabic languages and Hebrew are typical languages using the RTL directionality.

An image can have its dir attribute set to "rtl" in which case the HTML attributes title and alt will be formatted and defined as "rtl".

When a table has its dir set to "rtl", the column order is arranged from right to left.

When an element has its dir set to "auto", the direction of the element is determined based on its first strong directionality character, or default to the directionality of its parent element. (We may need to talk more about the algorithm in the spec here.)

Note: Prior to Gecko 7.0, it was possible for the returned value to not be all lower-case. Starting in Gecko 7.0, the returned strings are always all lower-case, as required by the specification.

Syntax

var CurrentWritingDirection = elementNodeReference.dir;
elementNodeReference.dir = NewWritingDirection;
  • CurrentWritingDirection is a string variable representing the text writing direction of the current element.
  • NewWritingDirection is a string variable representing the text writing direction value.

Possible values for dir are ltr, for left-to-right, and rtl, for right-to-left, and auto for specifying that the direction of the element must be determined based on the contents of the element.

Example

var parg = document.getElementById("para1"); 
parg.dir = "rtl"; 
// change the text direction on a paragraph identified as "para1"

Specification

Specification Status Comment
Document Object Model (DOM) Level 2 HTML Specification Recommendation  

See also

Document Tags and Contributors

 Contributors to this page: teoli
 Last updated by: teoli,