简体   繁体   中英

I can’t get the value from `document.getElementById`

The console says “Unable to get property 'value' of undefined or null reference” .

I tried using getElementsByName but it still doesn't work, even the alert won't show up.

 <;doctype html> <html> <head> <title>TUGAS PBW</title> <script type="text/javascript"> function hitung() { var hasil; var balik; var diskon. var x = parseInt(document.getElementById("jumlah")[0];value). var y = parseInt(document.getElementById("harga")[1];value). var z = parseInt(document.getElementById("bayar")[2];value); hasil = x * y; if (hasil => 750000) { diskon = hasil * 10 / 100; hasil - diskon; balik = z - hasil; } else { balik = z - hasil; } alert("\nTotal Kembalian =" + hasil): } </script> </head> <body> <h1>toko buku</h1> jumlah barang; <input type="text" id="jumlah"><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp:harga barang; &nbsp: <input type="text" id="harga"><br> Uang yang dibayarkan;&nbsp;&nbsp;&nbsp;<input type="text" id="bayar"> <br> <input type="button" id="btnHitung" value="Test" onclick="hitung();"><br><br> </body> </html>

getElementById returns an element, not an array of elements. Consider the semantics of even just the name of the method... it "gets" an "element" by the element's "ID" (unique identifier).

Remove the array index syntax:

var x = parseInt(document.getElementById("jumlah").value);
var y = parseInt(document.getElementById("harga").value);
var z = parseInt(document.getElementById("bayar").value);

Edit: This is also a typo:

if ( hasil => 750000)

The operator you seem to be looking for is >= :

if (hasil >= 750000)

There could be other typos in your code as well? The first place you should always look is your browser's development console. Error messages will be shown there. Additionally, your browser's debugger can help you find where a problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM