How to set the text direction with JavaScript?

JavascriptWeb DevelopmentFront End Scripts

To set the text direction, use the direction property with JavaScript. Set rtl for right-to-left or ltr for left-to-right text direction.


You can try to run the following code to set the text direction with JavaScript −

Live Demo

<!DOCTYPE html>
      <h1 id="myID">Heading 1</h1>
      <p>Click below to set the direction of heading to Left-to-right</p>
      <button type="button" onclick="display()">Click to change the direction</button>
         function display() {
            document.getElementById("myID").style.direction = "rtl";
Updated on 23-Jun-2020 12:22:14