JavaScript Code


jQuery – Right align text in element(s)



Right align text in HTML Element(s) using jQuery

Selected some HTML elements in the document, align the text in the elements to right side using jQuery.

Solution

To right align text in selected HTML elements using jQuery, call css() function on the selected elements and pass the arguments: "text-align" CSS property, and "right" value for the CSS property.

For example, the syntax to right align text in all paragraphs in the document is

$("p").css("text-align", "right");

Programs

1. Right align text in paragraphs in the document.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script>
      function myAction() {
        $("p").css("text-align", "right");
      }
    </script>
  </head>
  <body>
    <button onclick="myAction()">Right align text in paragraphs</button>
    <p style="background:yellow;">This is paragraph.</p>
  </body>
</html>


copyright @2023