Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My List that auto calculates prices by amount of product wont accept amount of product

I am trying to make a list where u can put in the amount of product u want and press a button and the Sum of all the products will be calculated, i am having trouble with the function to get the ammount for each product. I always get the error message when trying the button: Uncaught TypeError: document.getElementById(…) is null

<!DOCTYPE html>
<html>
<head>

<script>
let WaschlotionAmount;
let HaarshampooAmont;
let BodylotionAmount;
let WundschutzcremeAmount;
let DeosprayAmount;
let ZahncremeAmount;
let ZahnbuersteAmount;
let ZahnspuellbecherAmount;
let ZahnhaftcremeAmount;
let ZahnreinigunstabsAmount;
let RasierklingenAmount;
let RasiererMitKlingenAmount;
let RasierschaumAmount;
let EinwegrasiererAmount;

let Sum;
</script>
</head>
<body>
  <table>
    <tr>
      <th>Kosmetikartikel</th>
      <th>Menge</th>
    </tr>
    <tr>
      <td>Waschlotion</td>
      <td><input id="WaschlotionAmount"></td>
    </tr>
    <tr>
      <td>Haarshampoo</td>
      <td><input id="HaarshampooAmount"></td>
    </tr>
    <tr>
      <td>Bodylotion</td>
      <td><input id="BodylotionAmount"></td>
    </tr>
    <tr>
      <td>Wundschutzcreme</td>
      <td><input id="WundschutzcremeAmount"></td>
    </tr>
    <tr>
      <td>Deospray</td>
     <td><input id="DeosprayAmount"></td>
   </tr>
   <tr>
    <td>Zahncreme</td>
    <td><input id="ZahncremeAmount"></td>
  </tr>
  <tr>
   <td>ZahnbĂĽrste</td>
   <td><input id="ZahnbuersteAmount"></td>
 </tr>
 <tr>
  <td>ZahnspĂĽllbecher</td>
  <td><input id="ZahnspuellbecherAmount"></td>
</tr>
   <tr>
    <td>Zahnhaftcreme</td>
    <td><input id="ZahnhaftcremeAmount"></td>
  </tr>
  <tr>
    <td>Zahnreinigunstabs</td>
    <td><input id="ZahnreinigunstabsAmount"></td>
  </tr>
  <tr>
    <td>Rasierklingen</td>
    <td><input id="RasierklingenAmount"></td>
  </tr>
  <tr>
    <td>Rasierer mit Klingen</td>
    <td><input id="RasiererMitKlingenAmount"></td>
  </tr>
  <tr>
    <td>Rasierschaum</td>
    <td><input id="Rasierschaum"></td>
  </tr>
  <tr>
    <td>Einwegrasierer</td>
    <td><input id="EinwegrasiererAmount"></td>
  </tr>
  <tr>
    <td><b>Gesamtpreis</b></td>
    <td><p id="Sum"></p></td>
  </tr>
</table>
<button onclick="SetAmount()">Try me</button>
</body>
<script>
var Waschlotion = 1.30;
var Haarshampoo = 1.50;
var Bodylotion = 3;
var Wundschutzcreme = 4.80;
var Deospray = 1.80;
var Zahncreme = 3.20;
var Zahnbuerste = 1;
var Zahnspuellbecher = 1.25;
var Zahnhaftcreme = 1.50;
var Zahnreinigunstabs = 3.20;
var Rasierklingen = 2.49;
var RasiererMitKlingen = 4.50;
var Rasierschaum = 1.20;
var Einwegrasierer = 2.40;



function SetAmount() {
  let WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
  let HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
  let BodylotionAmount = document.getElementById("BodylotionAmount").value;
  let WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
  let DeosprayAmount = document.getElementById("DeosprayAmount").value;
  let ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
  let ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
  let ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
  let ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
  let ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
  let RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
  let RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
  let RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
  let EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
}

function Calculate() {
  document.getElementById("Sum").innerHTML = Sum;
}
 </script>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Change id of Rasierschaum to RasierschaumAmount. It will work!

 <!DOCTYPE html>
  <html>
    <body>
      <table>
        <tr>
          <th>Kosmetikartikel</th>
          <th>Menge</th>
        </tr>
        <tr>
          <td>Waschlotion</td>
          <td><input id="WaschlotionAmount"></td>
        </tr>
        <tr>
          <td>Haarshampoo</td>
          <td><input id="HaarshampooAmount"></td>
        </tr>
        <tr>
          <td>Bodylotion</td>
          <td><input id="BodylotionAmount"></td>
        </tr>
        <tr>
          <td>Wundschutzcreme</td>
          <td><input id="WundschutzcremeAmount"></td>
        </tr>
        <tr>
          <td>Deospray</td>
         <td><input id="DeosprayAmount"></td>
       </tr>
       <tr>
        <td>Zahncreme</td>
        <td><input id="ZahncremeAmount"></td>
      </tr>
      <tr>
       <td>ZahnbĂĽrste</td>
       <td><input id="ZahnbuersteAmount"></td>
     </tr>
     <tr>
      <td>ZahnspĂĽllbecher</td>
      <td><input id="ZahnspuellbecherAmount"></td>
    </tr>
       <tr>
        <td>Zahnhaftcreme</td>
        <td><input id="ZahnhaftcremeAmount"></td>
      </tr>
      <tr>
        <td>Zahnreinigunstabs</td>
        <td><input id="ZahnreinigunstabsAmount"></td>
      </tr>
      <tr>
        <td>Rasierklingen</td>
        <td><input id="RasierklingenAmount"></td>
      </tr>
      <tr>
        <td>Rasierer mit Klingen</td>
        <td><input id="RasiererMitKlingenAmount"></td>
      </tr>
      <tr>
        <td>Rasierschaum</td>
        <td><input id="RasierschaumAmount"></td>
      </tr>
      <tr>
        <td>Einwegrasierer</td>
        <td><input id="EinwegrasiererAmount"></td>
      </tr>
      <tr>
        <td><b>Gesamtpreis</b></td>
        <td><p id="Sum"></p></td>
      </tr>
    </table>
    <button onclick="SetAmount()">Try me</button>
    </body>
    <script>
    let Waschlotion = 1.30;
    let Haarshampoo = 1.50;
    let Bodylotion = 3;
    let Wundschutzcreme = 4.80;
    let Deospray = 1.80;
    let Zahncreme = 3.20;
    let Zahnbuerste = 1;
    let Zahnspuellbecher = 1.25;
    let Zahnhaftcreme = 1.50;
    let Zahnreinigunstabs = 3.20;
    let Rasierklingen = 2.49;
    let RasiererMitKlingen = 4.50;
    let Rasierschaum = 1.20;
    let Einwegrasierer = 2.40;



    function SetAmount() {
      let WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
      let HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
      let BodylotionAmount = document.getElementById("BodylotionAmount").value;
      let WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
      let DeosprayAmount = document.getElementById("DeosprayAmount").value;
      let ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
      let ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
      let ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
      let ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
      let ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
      let RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
      let RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
      let RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
      let EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
    }

    function Calculate() {
      document.getElementById("Sum").innerHTML = Sum;
    }
     </script>
    </html>

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading