Questo è un piccolo esempio, facilmente adattabile.
codice:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#form {
display: flex;
flex-direction: column;
}
#form>input,
button {
margin-top: 10px;
padding: 5px;
}
#anteprimaView {
padding: 20px;
color: white;
display: none;
position: absolute;
background-color: rgba(0, 0, 0, 0.9);
width: 400px;
height: 400px;
overflow: auto;
}
.view {
display: block !important;
}
</style>
</head>
<body>
<div id="container">
<div id="form-container">
<form action="salva_dati_inseriti.asp" id="form" method="POST">
<label for="nome">Nome</label>
<input type="text" name="nome" id="nome">
<label for="cognome">Cognome</label>
<input type="text" name="cognome" id="cognome">
<button type="button" id="anteprima">anteprima</button>
<input type="submit" value="Salva dati">
</form>
</div>
<div id="anteprimaView" class="">
<button id="close">Close</button>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
questo è il main.js
codice:
const form = document.getElementById('form')
const anteprima = document.getElementById('anteprima')
const anteprimaView = document.getElementById('anteprimaView')
const anteprimaClose = document.getElementById('close')
const label = ['Nome', 'Cognome']
anteprima.addEventListener('click', () => {
anteprimaView.classList.toggle('view')
for (let i = 0; i < form.length - 2; i++) {
let p = document.createElement("p")
let text = document.createTextNode(`${label[i]}: ${form[i].value}`)
p.append(text)
anteprimaView.append(p)
}
})
anteprimaClose.addEventListener('click', () => {
anteprimaView.classList.toggle('view')
for (let i = 0; i < form.length - 2; i++) {
anteprimaView.removeChild(anteprimaView.lastChild)
}
})