1-
21<!DOCTYPE html>
32< html lang ="en ">
43< head >
54< meta charset ="UTF-8 ">
65< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7- < title > Word counter </ title >
6+ < title > Word & Character Counter </ title >
87< style >
98* {
109 box-sizing : border-box;
9897</ head >
9998< body >
10099< div class ="container ">
101- < h1 > Word counter </ h1 >
100+ < h1 > Word & Character Counter </ h1 >
102101 < div id ="writing-container "> </ div >
103102 < button class ="add-btn " id ="addSection "> Add new section</ button >
104103</ div >
@@ -123,7 +122,8 @@ <h1>Word counter</h1>
123122 <textarea class="writing-area" placeholder="Start writing here...">${ content } </textarea>
124123 <div class="controls">
125124 <div class="stats">
126- Words: <span class="word-count">0</span>
125+ <span class="word-count">0</span> words ·
126+ <span class="char-count">0</span> characters
127127 <span class="save-status"></span>
128128 </div>
129129 <button class="remove-btn">Remove</button>
@@ -132,13 +132,14 @@ <h1>Word counter</h1>
132132
133133 const textarea = section . querySelector ( 'textarea' )
134134 const wordCount = section . querySelector ( '.word-count' )
135+ const charCount = section . querySelector ( '.char-count' )
135136 const saveStatus = section . querySelector ( '.save-status' )
136137 const removeBtn = section . querySelector ( '.remove-btn' )
137138
138- updateWordCount ( textarea , wordCount )
139+ updateCounts ( textarea , wordCount , charCount )
139140
140141 textarea . addEventListener ( 'input' , ( ) => {
141- updateWordCount ( textarea , wordCount )
142+ updateCounts ( textarea , wordCount , charCount )
142143 debouncedSave ( )
143144 } )
144145
@@ -154,9 +155,16 @@ <h1>Word counter</h1>
154155 return text . trim ( ) ? text . trim ( ) . split ( / \s + / ) . length : 0
155156}
156157
157- function updateWordCount ( textarea , wordCountElement ) {
158- const count = countWords ( textarea . value )
159- wordCountElement . textContent = count
158+ function countCharacters ( text ) {
159+ return text . length
160+ }
161+
162+ function updateCounts ( textarea , wordCountElement , charCountElement ) {
163+ const wordCount = countWords ( textarea . value )
164+ const charCount = countCharacters ( textarea . value )
165+
166+ wordCountElement . textContent = wordCount
167+ charCountElement . textContent = charCount
160168}
161169
162170function saveToLocalStorage ( ) {
0 commit comments