Word & Character Counter
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 100%;
text-align: center;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 8px;
margin-bottom: 20px;
resize: none;
outline: none;
transition: border-color 0.3s;
}
textarea:focus {
border-color: #4CAF50;
}
.counter-info {
margin-bottom: 20px;
}
.counter {
font-size: 18px;
color: #333;
font-weight: bold;
}
.warning-message {
font-size: 16px;
color: red;
font-weight: bold;
}
@media (max-width: 600px) {
.container {
padding: 15px;
}
h1 {
font-size: 20px;
}
.counter {
font-size: 16px;
}
}
function updateCounter() {
let text = document.getElementById('textInput').value;
// Count words (split by whitespace)
let wordCount = text.trim().split(/\s+/).filter(function (word) {
return word.length > 0;
}).length;
// Count characters (including spaces)
let charCount = text.length;
// Update the counters in the UI
document.getElementById('wordCount').textContent = `Words: ${wordCount}`;
document.getElementById('charCount').textContent = `Characters: ${charCount}`;
// Warning message if the character limit exceeds 200
if (charCount > 200) {
document.getElementById('warning').textContent = 'Warning: Character limit exceeded (200 characters)';
} else {
document.getElementById('warning').textContent = '';
}
}