0

Trying to make a website for buying movie tickets and store it in an array as well as showing them on screen but I can't figure out why it is not making an array. when I try to order a second order it overwrites the old object.

JS CODE:

function test() {
  const billett = [];

  let valgtFilm = document.getElementById("film").value;
  let antall = document.getElementById("antall").value;
  let fornavn = document.getElementById("forNavn").value;
  let etternavn = document.getElementById("etterNavn").value;
  let telefon = document.getElementById("telefornr").value;
  let epost = document.getElementById("epost").value;
  let tall = Number(antall);
  let telefonnr = Number(telefon);

  let ut = "<table><tr>" +
    "<th>Film</th><th>antall</th><th>Navn</th><th>Etternavn</th><th>Telefonnr</th><th>Epost</th>" +
    "</tr>";


  if (valgtFilm === "non") {
    alert("Feil1");
  }
  if (tall <= 0) {
    alert("Feil2");
  }
  if (fornavn === "") {
    alert("Feil3");
  }
  if (etternavn === "") {
    alert("Feil4");
  }
  if (isNaN(telefonnr) || telefon === "") {
    alert("Feil5");
  }
  if (epost === "") {
    alert("Feil6");
  } else {
    const nyBillett = {
      film: valgtFilm,
      antall: tall,
      navn: fornavn,
      etternavn: etternavn,
      tlf: telefonnr,
      epost: epost,
    }
    billett.push(nyBillett);
    for (let b of billett) {

      ut += "<tr>";
      ut += "<td>" + b.film + "</td><td>" + b.antall + "</td><td>" + b.navn + "</td><td>" + b.etternavn + "</td><td>" + b.tlf + "</td><td>" + b.epost + "</td>";
      ut += "</tr></br>";

      document.getElementById("feil").innerHTML = ut;

      //fjerner verdier
      document.getElementById("film").value = 'Velg film her';
      document.getElementById("antall").value = "";
      document.getElementById("forNavn").value = "";
      document.getElementById("etterNavn").value = "";
      document.getElementById("telefornr").value = "";
      document.getElementById("epost").value = "";
    }
  }
}
3
  • Please click edit, then [<>] snippet editor and provide a minimal reproducible example Commented Feb 17, 2021 at 17:48
  • document.getElementById("feil").innerHTML += ut; but you are missing a </table> Commented Feb 17, 2021 at 17:49
  • 1
    const billett = []; <-- lets define a new array each time. Commented Feb 17, 2021 at 17:50

1 Answer 1

2

Every time the function test is called a new array is created. The new array doesn't contain the objects pushed to the old array.

Declare the array outside the function.

let billett = [];

function test() {
  ....

  billett.push(nyBillett);

  ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh..... haha thanks! didn't notice that. its working now:D!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.