-1
<script>
      var level = ["Kindergarten", "ElemantarySchool"];
      var years = [];
      years[0] = ["1", "2", "3"];
      years[1] = ["1", "2", "3", "4", "5", "6"];

      for (var i = 0; i < level.length; i++){
           var levelOption = new Option(level[i], i);
           document.getElementById("level").appendChild(levelOption);
      }
      changeyears();
      function changeyears() {
           var yearsSelect = document.getElementById("years");
           while (yearsSelect.childNodes.length > 0) {
                  yearsSelect.removeChild(yearsSelect.childNodes[0]);
           }
           var yearsList = years[document.getElementById("level").selectedIndex];
           for (var i = 0; i < yearsList.length; i++) {
               document.getElementById("years").appendChild(new Option(yearsList[i], i));
           }
      }
</script>

I don't want value 0. I want both arrays to start from value 1.

<select name="years">
    <option value="0">1</option>
    <option value="0">1</option>
    <option value="0">1</option>
</select>
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • 1
    Post your working code in fiiddle.net and explain where you stuck. – Kishore Indraganti Sep 01 '15 at 04:40
  • 2
    you just replace: `document.getElementById("years").appendChild(new Option(yearsList[i], (i+1)));`, if i understand – Sherali Turdiyev Sep 01 '15 at 04:46
  • @ThadsanasitPeangkaew Welcome to StackOverflow. Once SheraliTurdiyev has written his answer, you can accept it (by clicking the checkbox next to his answer). Also, avoid "Thank you"-like comments; instead you can upvote answers (and questions) by clicking the up-arrow next to the question. – Zev Spitz Sep 01 '15 at 05:38

2 Answers2

1

Array index can't be changed from 0, we have to write a logic to access index in the way, while accessing you can manipulate it as per your requirement.

Why the array index should start at zero is not a trivial one and relates to interesting concepts from computer science. First of all, it has strong relation to language design. For example in C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language.

There is already lots of discussion for this can be found Why The Array Index Should Start From 0 and Why does the indexing start with zero in 'C'?

Community
  • 1
  • 1
Mukesh Singh Rathaur
  • 12,577
  • 2
  • 23
  • 24
  • True, but unlike most other languages, arrays in Javascript don't have special meaning beyond "objects with properties indexed by numbers starting with 0". Javascript has a simple mechanism for having an object start indexing with 1. – Zev Spitz Sep 01 '15 at 06:00
1

you just replace (i -> (i+1)):

<script>
      var level = ["Kindergarten", "ElemantarySchool"];
      var years = [];
      years[0] = ["1", "2", "3"];
      years[1] = ["1", "2", "3", "4", "5", "6"];

      for (var i = 0; i < level.length; i++){
           var levelOption = new Option(level[i], (i+1));
           document.getElementById("level").appendChild(levelOption);
      }
      changeyears();
      function changeyears() {
           var yearsSelect = document.getElementById("years");
           while (yearsSelect.childNodes.length > 0) {
                  yearsSelect.removeChild(yearsSelect.childNodes[0]);
           }
           var yearsList = years[document.getElementById("level").selectedIndex];
           for (var i = 0; i < yearsList.length; i++) {
               document.getElementById("years").appendChild(new Option(yearsList[i], (i+1)));
           }
      }
</script>
Sherali Turdiyev
  • 1,745
  • 16
  • 29