-2

So I’m working on a small project of my own in order to learn js/html/css, I’ve searched for hours but didn’t find anything,

Here’s what I wanna do:

  1. User inter text input “specified with an id”
  2. A button sends the text to a js function “carrying the same id”
  3. The function simply add the text into an existing array “also with the same id”

The hard part is to edit the original js file and save it as a permanent change, as when opining the page a js function takes that array elements and display them, so every single time I want to add a new element I have to edit the js file.

edit apparently it’s not possible to do this through js, or even permanently edit the js file, so what exactly should I use?

Axel
  • 19
  • 1
  • 2
    You didnt find anything because you never edit the original js file. You store it in a variable. `myarray.push(newvalue)` – Andrew Jun 11 '18 at 17:13
  • Possible duplicate of [Is it possible to write data to file using only JavaScript?](https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript) – Andrew Jun 11 '18 at 17:15
  • 1
    As Andrew pointed above, you would not edit the JS file because it is not a database for storage. Storing the values in an array will work but only in memory. If your goal is to have them exist after you leave the page and return to it, you need to be researching that instead. – suchislife Jun 11 '18 at 17:17
  • 1
    https://stackoverflow.com/questions/23743862/save-data-to-local-storage – Andrew Jun 11 '18 at 17:21
  • @Andrew what should I use then other than js to store and use data? – Axel Jun 11 '18 at 17:23
  • @Axel https://stackoverflow.com/questions/50802904/making-permanent-change-to-original-js-file?noredirect=1#comment88611966_50802904 – Andrew Jun 11 '18 at 17:31
  • @Andrew thanks man – Axel Jun 11 '18 at 17:32

1 Answers1

0

As mentioned in comments, you should be trying to make a permanent change to the javascript file, which trying to do via javascript is basically impossible: Is it possible to write data to file using only JavaScript?

You should either be storing the data in a variable like so:

myarray.push(newvalue)

But this clears when they refresh the page, so instead you could try using local storage, to store the data for future visits as well:

Save data to local storage

Andrew
  • 18,680
  • 13
  • 103
  • 118