screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> </head> <body> <div id="app"> <h1>Javascript Capitalize first letter of each word in string</h1> <p>String : font awesome icons</p> <p id="output"></p> <button id="capitalizeButton">Capitalize String</button> </div> <script> function capitalizeWords(str) { const words = str.split(" "); const capitalizedWords = words.map((word) => { const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); return capitalizedWord; }); const capitalizedString = capitalizedWords.join(" "); return capitalizedString; } function handleButtonClick() { const sentence = "font awesome icons"; const capitalizedSentence = capitalizeWords(sentence); const outputElement = document.getElementById("output"); outputElement.textContent = capitalizedSentence; } // Add event listener to the button const capitalizeButton = document.getElementById("capitalizeButton"); capitalizeButton.addEventListener("click", handleButtonClick); </script> </body> </html>