<header>
<h2 class="title">document.forms</h2>
<p class="description">Повертає колекцію всіх форм (form) документа.</p>
</header>
<main>
<div class="data">
<form action="">
<input type="text" name="login" placeholder="Логін" value="admin">
<input type="passwird" name="pass" placeholder="Пароль">
<input type="submit" value="OK">
</form>
<br>
</div>
<div class="result">
<button id="showForms">Показати інформацію про форми</button>
<div id="formsInfo"></div>
</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 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;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
button {
margin-bottom: 10px;
}
#formsInfo {
margin-top: 20px;
}
document.getElementById('showForms').addEventListener('click', function() {
const formsInfoContainer = document.getElementById('formsInfo');
formsInfoContainer.innerHTML = ''; // Очищення контейнера перед відображенням нових даних
Array.from(document.forms).forEach((form, index) => {
const formInfo = document.createElement('div');
formInfo.innerHTML = `<strong>Форма ${index + 1}:</strong> <br> ID: ${form.id || 'Немає'} <br> Action: ${form.action} <br>`;
const fieldsInfo = document.createElement('ul');
Array.from(form.elements).forEach((element) => {
const li = document.createElement('li');
li.textContent = `Поле: ${element.name || element.id || 'Без імені'}, Тип: ${element.type}, Значення: ${element.value}`;
fieldsInfo.appendChild(li);
});
formInfo.appendChild(fieldsInfo);
formsInfoContainer.appendChild(formInfo);
});
if (document.forms.length === 0) {
formsInfoContainer.innerHTML = 'На сторінці немає форм.';
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.