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);
            }
        }
4
  • You can't do that, at least in pure browser JS. Why would you want to do that? Commented Jan 16, 2018 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 . Commented Jan 16, 2018 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. Commented Jan 16, 2018 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 Commented Jan 16, 2018 at 15:43

1 Answer 1

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

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.