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.


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

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

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

         function display() {
            document.getElementById("myID").style.textTransform = "capitalize";

Updated on: 23-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started