简体   繁体   中英

Javascript - Accessing a global array in a DOM event

I'm currently working on a links generator in order to improve myself. I've had trouble accessing a global array declared at the beginning of my script inside a "click" event since day one. I've tried to debug it myself but am stuck!

Currently I'm trying to add new data from a form at the beginning of the array with the unshift() function. The return of the function tells me that there are 4 items but the data isn't inside the array.

It looks like this :

var listeLiens = [{
    titre: "So Foot",
    url: "http://sofoot.com",
    auteur: "yann.usaille"
  },
  {
    titre: "Guide d'autodéfense numérique",
    url: "http://guide.boum.org",
    auteur: "paulochon"
  },
  {
    titre: "L'encyclopédie en ligne Wikipedia",
    url: "http://Wikipedia.org",
    auteur: "annie.zette"
  }
];

inputSubmit.addEventListener("click", function() {
  form = document.querySelector("form");
  listeLiens.unshift({
    "titre": form.elements.titreLien.value,
    "url": form.elements.urlLien.value,
    "auteur": form.elements.nomAuteur.value
  });
});

I checked on the web but couldn't find a solution.

I tried some things to solve the problem but it stills the same mistake ! I don't suceed in accessing to the global array declared at the beginning of my script in my event... it seems to be another array used in the event, not the same as at the beggining of the script...

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