Javascript DOM HTML Element dir Property set

Introduction

The dir property sets or gets the value of the dir attribute of an element.

Change the text direction of a <p> element to "right-to-left":

document.getElementById("myP").dir = "rtl";

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">Click the button to change my text direction.</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from  w w w  .ja  v  a  2 s .  c  om*/
  document.getElementById("myP").dir = "rtl";
}
</script>

</body>
</html>

The dir attribute specifies the text-direction (reading order) of the element's content.

Property Values

Value Description
ltr Default. Left-to-right text direction
rtl Right-to-left text direction
auto browser chooses the text direction based on the content

The dir property returns a String representing the text writing direction of the element.




PreviousNext

Related