Il 1° codice, preso dal Web, con i due parametri p1 e p2 assegnati dal programma stesso, funziona regolarmente.
Nel 2° codice ho apportato una variante: i due parametri p1 e p2 li assegno da tastiera; non viene segnalato nessun errore, ma il codice non funziona più e dà sempre 0.
Desidererei sapere perché. Grazie
lanvoel
1° codice:
codice:<!DOCTYPE html> <html> <body> <p>Click the button to perform a calculation.</p> <button id="myBtn">Try it</button> <p id="demo"></p> <script> let p1 = 5; let p2 = 7; document.getElementById("myBtn").addEventListener("click", function() { myFunction(p1, p2); }); function myFunction(a, b) { document.getElementById("demo").innerHTML = a * b; } </script> </body> </html>
2° codice:
codice:<!DOCTYPE html> <html> <body> scrivi due numeri: <br> a= <input type="text" id="a" name="a" size=10;> b= <input type="text" id="b" name="b" size=10;> <p>Click the button to perform a calculation.</p> <button id="myBtn">Try it</button> <p id="demo"></p> <script> let p1 =a.value; let p2 =b.value; document.getElementById("myBtn").addEventListener("click", function() { myFunction(p1, p2); }); function myFunction(a, b) { document.getElementById("demo").innerHTML = a*b; } </script> </body> </html>

Rispondi quotando