Im new to JS but I think the post title does its job. From what i saw by googling this problem, there’s lots of ways to solve this issue. But basically, theres a text input field, the user writes ‘paris’. how do i make ‘paris’ turn into ‘Paris’. I need to get the input.value and change it. Obviously, i dont hardcode the sentence, the user chooses the word and i get it as a text input value.
let cityName = inputCity.value; let latestSearch = firstUpperCase(cityName); function firstUpperCase(cityName){ //Assign touppercase() to first letter of string, then add the rest of the sentence by using the actual sentence with the first letter sliced. latestSearch = cityName[0].toUpperCase(); return latestSearch; }
Advertisement
Answer
For your question what I understand truely, you want to uppercase first letter of input string that may or may not contain more than one word. Following this, this code might help
let userData = "lorem ipsum the world" let newString = userData[0].toUpperCase() + userData.slice(1) alert(newString)
In case you need every first letter of each word uppercase, this might help
let userData = "lorem ipsum the world" userData = userData.split(" ") userData.forEach(function(word, index){ userData[index] = word[0].toUpperCase() + word.slice(1) }) newString = userData.join(" ") alert(newString)