Web Analytics Made Easy -
StatCounter referencing <select> options from javascript - CodingForum

Announcement

Collapse
No announcement yet.

referencing <select> options from javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • referencing <select> options from javascript

    Hi, I am trying to reference the values of individual options of a select box, from a javascript function. So far I have this:
    Code:
    skill_array=document.getElementById('skillID[]').value;
      for (var i=0; i<skill_array.length; i++){
        alert(skill_array[i]);
      }
    where skillID[] occurs in:
    Code:
    <select name='skillID[]' id='skillID[]' size='5' multiple>
    It knows the length of the array but individual values are undefined. Do i need to do something special with id's on each of the <option>'s?

    Thanks.

  • #2
    well, not sure if this helps, but have you set values behind the text options?

    ie. <option value="1">Value1</option>


    also, when referencing the element, use just:
    skill_array=document.getElementById('skillID[]')

    Now you have an array which identifies all the options within. This should iterate through each of the options values (assuming you've set them)

    for (var i=0; i<skill_array.length; i++) {
    alert(skill_array.options[i].value)}

    Comment


    • #3
      Yup, thats sorted it - brilliant!
      Cheers mate. Jus had my .value in the wrong place

      Comment

      Working...
      X