0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<blockquote><p>Meow! People talk too much, and the comment section should not be longer than the news story itself!</p>
1
<blockquote><p>Meow! People talk too much, and the comment section should not be longer than the news story itself!</p>
2
</blockquote><p>This is our new assignment from the boss. The commenting system that we created in the<a>previous chapter</a>turned out to be quite convenient for users. Perhaps too convenient. They now eagerly discuss new stories… and sometimes they get a little carried away. The boss wants the comments on the site to be no longer than 142 characters. The web designer added a counter to the comment form so that users could see how many characters they had already used. If the character limit is exceeded, then the comment “Submit” button should be locked, and the character counter and text in the input field will turn red.</p>
2
</blockquote><p>This is our new assignment from the boss. The commenting system that we created in the<a>previous chapter</a>turned out to be quite convenient for users. Perhaps too convenient. They now eagerly discuss new stories… and sometimes they get a little carried away. The boss wants the comments on the site to be no longer than 142 characters. The web designer added a counter to the comment form so that users could see how many characters they had already used. If the character limit is exceeded, then the comment “Submit” button should be locked, and the character counter and text in the input field will turn red.</p>
3
<p>We need to finish our script!</p>
3
<p>We need to finish our script!</p>
4
<p>We already know how to <a>get data from the input field</a>, but only after the form has been submitted. Now we need to determine how the comment length can be measured before the form is sent. How can we accomplish that? We will use the oninput event handler. Instructions inside the oninput handler are executed every time the value in the input field changes. For example:</p>
4
<p>We already know how to <a>get data from the input field</a>, but only after the form has been submitted. Now we need to determine how the comment length can be measured before the form is sent. How can we accomplish that? We will use the oninput event handler. Instructions inside the oninput handler are executed every time the value in the input field changes. For example:</p>
5
// Find the input field let textarea = document.querySelector('textarea'); // Add the event handler textarea.oninput = function () { // Display data from the input field console.log(textarea.value); };<p>Type the word “Muffin”, then remove the last character and take a look at the console:</p>
5
// Find the input field let textarea = document.querySelector('textarea'); // Add the event handler textarea.oninput = function () { // Display data from the input field console.log(textarea.value); };<p>Type the word “Muffin”, then remove the last character and take a look at the console:</p>
6
<p>M (String)</p>
6
<p>M (String)</p>
7
<p>Mu (String)</p>
7
<p>Mu (String)</p>
8
<p>Muf (String)</p>
8
<p>Muf (String)</p>
9
<p>Muff (String)</p>
9
<p>Muff (String)</p>
10
<p>Muf (String)</p>
10
<p>Muf (String)</p>
11
<p>Notice how the text from the input field was displayed in the console with each change, including both when new characters were added and deleted.</p>
11
<p>Notice how the text from the input field was displayed in the console with each change, including both when new characters were added and deleted.</p>
12
<p>Thanks to the oninput event handler, we can get the comment text before the user submits it. Let’s see how it works: add a handler to the comment input field and enter some text.</p>
12
<p>Thanks to the oninput event handler, we can get the comment text before the user submits it. Let’s see how it works: add a handler to the comment input field and enter some text.</p>