Código: Selecionar todos
<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cadastro de Clientes</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<style>
.align {
margin: 20px;
}
</style>
</head>
<body>
<div class="align">
<h2>Formulário de Cadastro</h2>
<form id="cadastroForm">
<div class="mb-3">
<label for="f_nome" class="form-label">Nome:*</label>
<input type="text" class="form-control" id="f_nome" required placeholder="Nome">
</div>
<div class="mb-3">
<label for="f_idade" class="form-label">Idade:*</label>
<input type="number" class="form-control" id="f_idade" required placeholder="Idade">
</div>
<div class="mb-3">
<label for="f_email" class="form-label">E-mail:*</label>
<input type="email" class="form-control" id="f_email" required placeholder="E-mail">
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
<button type="button" class="btn btn-secondary" id="clearTable">Limpar Tabela</button>
</form>
</div>
<div class="align">
<h2>Lista de Clientes</h2>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nome</th>
<th scope="col">Idade</th>
<th scope="col">E-mail</th>
</tr>
</thead>
<tbody id="clientList"></tbody>
</table>
<div id="successMessage" class="alert alert-success" style="display: none;"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script>
document.getElementById('cadastroForm').addEventListener('submit', function(event) {
event.preventDefault();
const nome = document.getElementById('f_nome').value;
const idade = document.getElementById('f_idade').value;
const email = document.getElementById('f_email').value;
const clientList = document.getElementById('clientList');
const rowCount = clientList.rows.length + 1;
const newRow = clientList.insertRow();
newRow.innerHTML = `
<th scope="row">${rowCount}</th>
<td>${nome}</td>
<td>${idade}</td>
<td>${email}</td>
`;
document.getElementById('successMessage').innerText = 'Cadastro realizado com sucesso!';
document.getElementById('successMessage').style.display = 'block';
this.reset(); // Limpa o formulário
});
document.getElementById('clearTable').addEventListener('click', function() {
document.getElementById('clientList').innerHTML = '';
document.getElementById('successMessage').style.display = 'none'; // Oculta a mensagem
});
</script>
</body>
</html>