0

If I'm reading data "array" from javascript file, and I want to change an element value in the array, how can I apply this update on the javascript file "save the javascript file with new value".

var country = document.getElementById('edit_country').value;
        for (i = 0; i < country_arr.length; i++) {
            if(country_arr[i] == country) {
                var city = "|".concat(document.getElementById("add_city").value);
                city_arr[i] = city_arr[i].concat(city);
            }
        }
  • *You can't do that, at least in pure browser JS*. Why would you want to do that? – Adam Azad Jan 16 '18 at 15:21
  • I am using a dropdown list for selecting country and city, I am reading the countries and cities from a javascript file I want to have an ability to add new cities or edit some cities . – George Saad Jan 16 '18 at 15:32
  • You should go with a database then, or simply store these in a minimal JSON file. Either way, you will have to rely on server-side to securely update the database/JSON. – Adam Azad Jan 16 '18 at 15:34
  • using database is an the easy choice, but I thought inserting a 245 countries and 48300 cities in the database is a bad idea – George Saad Jan 16 '18 at 15:43

1 Answers1

0

You cannot edit a file from a computer with Java Script, the best thing you can do is to create a new file with it and download it. To download a file containing a JSON check this other question: Link

Daniel Diment
  • 110
  • 1
  • 8
  • I need to keep the data in javascript file, so I overrite the file with php and problem solve, not easy choise but its ok.\n thanks for your help – George Saad Jan 17 '18 at 12:59