How to set the capitalization of a text with JavaScript?



To set the capitalization, use the textTransform property in JavaScript. Set it to capitalize, if you want the first letter of every word to be a capital letter.

Example

You can try to run the following code to return the capitalization of a text with JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <button onclick = "display()">Capitalize Text</button>

      <div id = "myID">
         This is demo text! This is demo text!
      </div>

      <script>
         function display() {
            document.getElementById("myID").style.textTransform = "capitalize";
         }
      </script>
   </body>
</html>

Advertisements