<header>
<h2 class="title">keyup</h2>
<p class="description">Спрацьовує при відпусканні клавіші, щоб обробляти введені дані в реальному часі.</p>
</header>
<main>
<div class="result">
<label for="textInput">Введіть текст (макс. 100 символів):</label>
<textarea id="textInput" rows="4" placeholder="Введіть текст тут..."></textarea>
<p id="charCount">0 / 100 символів</p>
<p id="warningMessage" style="color: red; display: none;">Досягнуто ліміт символів!</p>
</div>
</main>
body {
font-size: 16px;
line-height: 1.5;
font-family: monospace;
}
header {
background-color: #f1f1f1;
margin-bottom: 25px;
padding: 15px;
box-shadow: 0px 0px 3px rgba(118, 118, 118, 1);
}
header h2.title {
padding-bottom: 15px;
border-bottom: 1px solid #999;
}
header p.description {
font-style: italic;
color: #222;
}
.result {
background-color: #f8f8f8;
padding: 15px;
box-shadow: 0px 0px 3px rgba(118, 118, 118, 1);
}
#textInput {
padding: 10px;
font-size: 16px;
width: 100%;
margin-bottom: 10px;
}
#charCount {
font-size: 14px;
color: #333;
}
#warningMessage {
font-size: 14px;
margin-top: 5px;
}
const textInput = document.getElementById('textInput');
const charCount = document.getElementById('charCount');
const warningMessage = document.getElementById('warningMessage');
const maxChars = 100;
textInput.addEventListener('keyup', function(event) {
const currentLength = textInput.value.length;
// Оновлення лічильника символів
charCount.textContent = `${currentLength} / ${maxChars} символів`;
// Якщо досягнуто ліміту, показуємо попередження
if (currentLength >= maxChars) {
warningMessage.style.display = 'block';
textInput.value = textInput.value.slice(0, maxChars); // Обрізаємо текст до ліміту
} else {
warningMessage.style.display = 'none';
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.