-2

I want this: Example: stackoverflow is helpful. => Stackoverflow is helpful.

As example show want to convert my first word first character to Upper Case. I tried the code given below which not working, not understanding what I am doing wrong please help.

<textarea autocomplete="off" cols="30" id="TextInput" name="message" oninput="myFunction()" rows="10" style="width: 100%;"></textarea>

<input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital!" /> </br>
</br>

<script>
  function FistWordFirstCharcterCapital() {
    var x = document.getElementById("TextInput").value.replace(string[0], string[0].toUpperCase());
    document.getElementById("TextInput").value = x;
  }
</script>
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    where's the code of variable `string`? – Ryan Nghiem May 16 '19 at 08:33
  • 6
    There is not `string` variable in your code. Get the `value` to the `string` variable first and then replace: `var string = document.getElementById("TextInput").value; var x = string.replace(string[0], string[0].toUpperCase());` – adiga May 16 '19 at 08:34
  • As a side note, `oninput="myFunction()"` doesn't realle make much sense, since there is no `myFunction` declaration anywhere. – briosheje May 16 '19 at 08:35
  • 2
    Possible duplicate of [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – Kévin Bibollet May 16 '19 at 08:36

5 Answers5

3

Treat it like an array-like object to get the first character, uppercase it, then concat it to the rest of the string:

const str = "hello World!";
const upper = ([c, ...r]) => c.toUpperCase().concat(...r);
console.log(upper(str));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

Just use this method

function jsUcfirst(string) 
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}
S.I.
  • 3,250
  • 12
  • 48
  • 77
Majid Azarniush
  • 643
  • 1
  • 6
  • 13
0

You can use charAt to get the first letter:

const string = "stackoverflow is helpful."
const capitalizedString = string.charAt(0).toUpperCase() + string.slice(1)

console.log(capitalizedString)
Kobe
  • 6,226
  • 1
  • 14
  • 35
0

You'd not want to use replace, nor something like string[0]. Instead, use below little method

const s = 'foo bar baz';

function ucFirst(str) {
  return str.substr(0, 1).toUpperCase() + str.substr(1);
}

console.log(ucFirst(s));
baao
  • 71,625
  • 17
  • 143
  • 203
  • Or koban's way, or one of the hundreds different solutions to that simple problem. @MaheerAli I showed one option, there are many though. Instead of yours, I'd prefer koban's though – baao May 16 '19 at 08:40
0

Since you seem to beggin with JS here is a detailed example :

function FistWordFirstCharcterCapital() {
  let text =  document.getElementById("TextInput").value;
  let firstSpaceIndex = text.indexOf(" ")!=-1 ? text.indexOf(" ")+1:text.length;
  let firstWord = text.substr(0, firstSpaceIndex);
  let firstWordUpper = firstWord.charAt(0).toUpperCase() + firstWord.slice(1)
  document.getElementById("TextInput").value = firstWordUpper + text.substr(firstSpaceIndex);;
}
<textarea autocomplete="off" cols="30" id="TextInput" name="message" rows="10" style="width: 100%;">
</textarea>

<input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital!" />
PopHip
  • 700
  • 6
  • 23