Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h2 class="title">UI Events</h2>
  <p class="description">Взаємодія з користувачем через події форми та фокусування.</p>
</header>
<main>
  <div class="result">
    <!-- Форма для взаємодії -->
    <form id="interactiveForm">
      <label for="username">Ім'я користувача:</label>
      <input type="text" id="username" name="username" maxlength="20" placeholder="Введіть ім'я">
      <small id="charCount">Залишилося 20 символів</small>

      <label for="email">Електронна пошта:</label>
      <input type="email" id="email" name="email" placeholder="Введіть email">

      <button type="submit">Надіслати</button>
    </form>
    <!-- Місце для повідомлення -->
    <p id="message" style="margin-top: 20px; color: #222;"></p>
  </div>
</main>

              
            
!

CSS

              
                body {
  font-size: 16px;
  line-height: 1.5;
  font-family: monospace;
}

header {
  background-color: #f1f1f1;
  margin-bottom: 25px;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px 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;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

/* Стилі для форми */
#interactiveForm {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

#interactiveForm input {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

#interactiveForm input:focus {
  border-color: #007BFF;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

#charCount {
  font-size: 0.9rem;
  color: #555;
}

              
            
!

JS

              
                // Отримуємо елементи
const form = document.getElementById('interactiveForm');
const usernameInput = document.getElementById('username');
const charCount = document.getElementById('charCount');
const message = document.getElementById('message');

// Відстежуємо подію 'input' для оновлення лічильника символів
usernameInput.addEventListener('input', (event) => {
  const remaining = usernameInput.maxLength - usernameInput.value.length;
  charCount.textContent = `Залишилося ${remaining} символів`;
});

// Фокус і втрата фокусу на полі
usernameInput.addEventListener('focus', () => {
  charCount.style.color = '#007BFF';
});

usernameInput.addEventListener('blur', () => {
  charCount.style.color = '#555';
});

// Обробка події надсилання форми
form.addEventListener('submit', (event) => {
  event.preventDefault(); // Зупиняємо стандартну дію
  const username = usernameInput.value.trim();
  const email = document.getElementById('email').value.trim();

  if (username && email) {
    message.textContent = `Дякуємо, ${username}! Ваша форма надіслана.`;
    message.style.color = 'green';
    form.reset();
    charCount.textContent = `Залишилося ${usernameInput.maxLength} символів`;
  } else {
    message.textContent = 'Будь ласка, заповніть всі поля.';
    message.style.color = 'red';
  }
});

              
            
!
999px

Console