Jeżeli masz <input type="reset"> w "obrębie" <form>, pola typu <input type="text"> są "resetowane" bez dodatkowego przypisywania zdarzenia do przycisku <input type="reset">.
<input type="reset" value="RESETUJ" style="cursor: pointer;">
Chcę aby po wpisaniu liczb w pola a i h pokazał mi wynik w polu P. Następnie po wpisaniu liczb w tych samych polach a i P pokazał mi wynik w polu h.
możesz użyć warunków
przykład
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="pole_trojkata.js"></script>
</head>
<body>
<form id="licz" action="" method="post" autocomplete="off">
<div style="padding-left: 80px;">
<h3>Wysokość Trójkąta </h3>
<p>h = (2 * P) / a</p><br>
<h3>Pole Trójkąta </h3>
<p>P = (a * h) / 2</p><br>
<p>Bok a <input type="text" id="a"></p>
<p>Pole P <input type="text" id="P"></p>
<p>Wysokość h <input type="text" id="h"></p>
<input type="button" value="Oblicz" style="cursor: pointer;" onclick="oblicz()">
<input type="reset" value="RESETUJ" style="cursor: pointer;">
</div>
</form>
</body>
</html>
let last_P, last_h;
function oblicz() {
const a = parseInt(document.getElementById('a').value),
h = parseInt(document.getElementById('h').value),
P = parseInt(document.getElementById('P').value);
if ((P && a) && last_P != P) {
document.getElementById('h').value = (2 * P) / a;
last_P = P;
}
if ((a && h) && last_h != h) {
document.getElementById('P').value = (a * h) / 2;
last_h = h;
}
return false;
}