HTML | DOM dir Property

The DOM dir Property is used to set or return the value of dir attribute in an Element. It defines a direction of the text in an Element Content.

Syntax

HTMLElementObject.dir

Return Values: It returns the the direction of the text.

HTMLElementObject.dir = "ltr|rtl|auto"

Properties:

  • ltr: It is a default Value and used to return the text-direction towards left-to-right.
  • rtl: It is used to return the right-to-left text-direction.

Example-1: To change the direction of the text.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM dir Property
    </title>
    <style>
        button {
            margin-left: 270px;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green;font-weight:bold;text-align:center;">
      GeeksForGeeks
    </h1>
    <h2 style="color:green;font-weight:bold;text-align:center"
      DOM dir Property
    </h2>
    <p id="sudo">
        <b>
          GeeksForGeeks is a good website for 
          learning computer science.
        </b>
    </p>
    <button onclick="geeks()">Submit</button>
  
    <script>
        function geeks() {
            document.getElementById("sudo").dir = "rtl";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

Example-2: To get the direction of the text.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML | DOM dir Property
  
    </title>
    <style>
        button {
            margin-left: 270px;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green;font-weight:bold;text-align:center;">
      GeeksForGeeks
    </h1>
    <h2 style="color:green;font-weight:bold;text-align:center">
      DOM dir Property
    </h2>
    <p id="sudo" dir="ltr">
        <b>
        GeeksforGeeks is a good website for
        learning computer science.
        </b>
    </p>
  
    <button onclick="geeks()">Submit</button>
    <p id="gfg" style="margin-left:270px;"></p>
    <script>
        function geeks() {
            var x = document.getElementById("sudo").dir;
            document.getElementById("gfg").innerHTML =
                "Direction of the text is " + x;
  
        }
    </script>
  
</body>
  
</html>          

chevron_right


Output:

Supported Browsers: The browser supported by DOM Dir property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.