// use strict mode, more errors will show this way. "use strict"; var capitalize = function(element) { // get the inner Text and turn it into an array, spliting all the spaces var text = element.innerText.split(' '), // result string result = ''; // for every element of the new Text array text.forEach(function(word) { // get the first letter var firstLetter = word.slice(0,1), // and all remaining letters remaining = word.slice(1); // then simply use toUpperCase() on // the first letter and add back the remaining part, also // add a space to the end of the string result += firstLetter.toUpperCase() + remaining + ' '; }); // set the elements inner text to the result element.innerText = result; // return the result, this way it can also be easily logged using console.log() return result; } var demoText = document.querySelector('#target'), demoTextSingle = document.querySelector('#single'); capitalize(demoText); capitalize(demoTextSingle);