0

ok so i have 2 arrays and i have to add a gui in html javascript that will allow a user to type in a new country and population and have a button that will add the new elements in to the arrays. how can i do that?

<head>
<title></title>
<script type="text/javascript">

    var countries = new Array();
    countries[0] = "Nigeria";
    countries[1] = "Irag";
    countries[2] = "Philippines";
    countries[3] = "Indonesia";
    countries[4] = "Egypt";
    countries[5] = "Russia";
    countries[6] = "Vietnam";
    countries[7] = "Ethiopia";
    countries[8] = "Mexico";

    var population = new Array();
    population[0] = "166,629,000";
    population[1] = "33,330,000";
    population[2] = "92,337,000";
    population[3] = "237,641,000";
    population[4] = "82,940,000";
    population[5] = "143,300,000";
    population[6] = "87,840,000";
    population[7] = "84,320,000";
    population[8] = "112,336,000";

</script>

1
  • What have you actually tried? Commented Dec 12, 2012 at 22:00

1 Answer 1

1

This should do the trick:

//Easier way to define and populate an array
var countries =
[
    "Nigeria",
    "Iraq",
    "Philippines",
    //And so on
];

//Same for population

//Function to add the new data
function addData()
{
    //push() adds an element at the end of an array
    countries.push(document.getElementById("myCountry").value);
    population.push(document.getElementById("myPopulation").value);
}

//Add the click event
document.getElementById("myButton").onclick = addData;
Sign up to request clarification or add additional context in comments.

Comments

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.