Word & Character Counter
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
background-color: #ffffff;
}
h1 {
text-align: center;
color: #4CAF50;
}
textarea {
width: 100%;
min-height: 150px;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.counter {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
p {
color: #666;
}
#wordCount,
#charCount {
font-weight: bold;
color: #333;
}
const textarea = document.getElementById('inputText');
const wordCountSpan = document.getElementById('wordCount');
const charCountSpan = document.getElementById('charCount');
textarea.addEventListener('input', updateCounter);
function updateCounter() {
const text = textarea.value;
const wordCount = countWords(text);
const charCount = text.length;
wordCountSpan.textContent = wordCount;
charCountSpan.textContent = charCount;
}
function countWords(text) {
const trimmedText = text.trim();
const wordArray = trimmedText === '' ? [] : trimmedText.split(/\s+/);
return wordArray.length;
}
// Initial update on page load
updateCounter();
0 Comments