In this tutorial we will learn how to Capitalize First Letter of String with JavaScript. JavaScript and HTML codes are give below.
Table of Contents
HTML Code
HTML Code is given below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Capitalize First Letter of Word with JavaScript</title>
</head>
<body>
<input type="text" id="input">
<button onclick="capitalize()">Capitalize first letter</button>
<script src="script.js"></script>
</body>
</html>
JavaScript Code
Take a look at the JavaScript code given below.
<script>
function capitalize(){
var string = document.getElementById('input').value;
var firstWord = string.charAt(0);
firstWord = firstWord.toUpperCase();
var restOfString = string.slice(1);
var newString = firstWord + restOfString;
alert(newString);
}
</script>
Video Tutorial
Watch video tutorial on How To Capitalize First Letter of Word with JavaScript.