0

I hope that someone would be able to help me to solve this simple problem.

My goal is to get the value in the array from selection drop down list.

Basically, I create an Array in Javascript and a selection drop down list in the body.

<script type="text/javascript">
    var even = new Array(2, 4, 6);
</script>
.
.
.
<select id="evenNumbers">
     <option value="1">two</option>
     <option value="2">four</option>
     <option value="3">six</option>
</select>

My question is how can I get the value in the Array if I select the option from the drop down list? e.g. when I select "two" from the drop down list, but I can get the value of "2" in the array in order to do some calculation.

2 Answers 2

1

your select box will look like this

<select >
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

if you want to populate values putting a for loop.

for getting selected option value

this.options[this.selectedIndex].value

http://awesomerails.wordpress.com/2007/12/04/get-the-value-of-a-selected-option-with-javascript/

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

Comments

0
document.getElementById('evenNumbers').onchange = function() {
  var index = this.value - 1; // array indices start at 0
  alert(even[index]);
}

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.