0

Here is my code, the problem I have is the alert just isnt showing, however if the alert is put in the while loop it shows but outside the loop it doesn't could someone explain why?

<script>

function GetText() {
    var name = document.getElementById("name").value;
    var age = document.getElementById("age").value;
    var house = document.getElementById("house").value;
    var road = document.getElementById("road").value;
    var city = document.getElementById("city").value;
    var county = document.getElementById("county").value;
    var country = document.getElementById("country").value;
    var postCode = document.getElementById("postCode").value;
    var activityNum = ["a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19"];
    var activityName = ["Rock climbing","Zipwire","Assault course","Orienteering","Raft Building","Team Challenges","Forest adventure walks","Tree climbing","High ropes zip wire","Night hill walks","Extreme rafting","High ropes","Tunnel mazes","Rock climbing on cliff","Cave exploring","Army assault course","Forest fitness test","All day mountain biking","Bungee jumping"];
    var theirChoices = [];
    var x = 0;
    while (x < 20) {
        choice = document.getElementById(activityNum[x]).checked;
        if (choice == "1") {
            var choice = activityName[x];
            theirChoices.push(choice);
            x++;
        } else {
            x++;
        }
    }
    alert(theirChoices);
}

</script>

<input type="checkBox" id="a1" size="50" />Rock climbing<br>
<input type="checkBox" id="a2" size="50"/>Zipwire<br>
<input type="checkBox" id="a3" size="50"/>Assault course<br>
<input type="checkBox" id="a4" size="50"/>Orienteering<br>
<input type="checkBox" id="a5" size="50"/>Raft Building<br>
<input type="checkBox" id="a6" size="50"/>Team Challenges<br>
<input type="checkBox" id="a7" size="50"/>Forest adventure walks<br>
<input type="checkBox" id="a8" size="50"/>Tree climbing<br>
<input type="checkBox" id="a9" size="50"/>High ropes zip wire<br>
<input type="checkBox" id="a10" size="50"/>Night hill walks<br>
<input type="checkBox" id="a11" size="50"/>Extreme rafting<br>
<input type="checkBox" id="a12" size="50"/>High ropes<br>
<input type="checkBox" id="a13" size="50"/>Tunnel mazes<br>
<input type="checkBox" id="a14" size="50"/>Rock climbing on cliff<br>
<input type="checkBox" id="a15" size="50"/>Cave exploring<br>
<input type="checkBox" id="a16" size="50"/>Army assault course<br>
<input type="checkBox" id="a17" size="50"/>Forest fitness test<br>
<input type="checkBox" id="a18" size="50"/>All day mountain biking<br>
<input type="checkBox" id="a19" size="50"/>Bungee jumping<br>
<input type="button" value="Submit All" size="50" onClick="GetText()"/><br>
2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by Unimportant
1

You have 19 activities but you're trying to access the 20th one, which crashes your script.
Counting starts from 0.

Also, feel free to increment x outside of the if statement, since you increment it either way.

if (choice == "1")
  theirChoices.push(activityName[x]);
++x;

Edited by Unimportant

Votes + Comments
Fixed it now, thanks alot!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.