Created
August 24, 2023 13:00
-
-
Save solanoize/d208bdb284313507ddc3a3ca0ec29dde to your computer and use it in GitHub Desktop.
pertemuan4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <div> | |
| <input type="number" id="angka1" > | |
| </div> | |
| <div> | |
| <input type="number" id="angka2" > | |
| </div> | |
| <div> | |
| <button>Hitung</button> | |
| </div> | |
| <div> | |
| Hasil = <span></span> | |
| </div> | |
| <script src="./script.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. event listener | |
| let buttonPenjumlahan = document.querySelector("button"); | |
| // addEventListener untuk penjumlahan | |
| buttonPenjumlahan.addEventListener("click", function () { | |
| let inputNumber1 = document.querySelector("#angka1"); | |
| let inputNumber2 = document.querySelector("#angka2"); | |
| // untuk mengambil nilai dari objek input, gunakan | |
| // properti .value | |
| // menampilkan hasil perhitungan ke dalam span | |
| // 1. kaitkan elemen span ke js | |
| let spanHasil = document.querySelector("span"); | |
| let hasilHitung = parseInt(inputNumber1.value) - parseInt(inputNumber2.value); | |
| if (hasilHitung || hasilHitung === 0) { | |
| spanHasil.innerHTML = hasilHitung; | |
| } else { | |
| spanHasil.innerHTML = "Perhitungan tidak valid"; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment