• Welcome to Valhalla Legends Archive.
 

Javascript, SELECT value array

Started by Grok, January 26, 2005, 01:48 PM

Previous topic - Next topic

Grok

When a SELECT is not "multiselect", getting its value attribute (the item selected) is easy.  Clicking on a name entry in this example will show the associated value.

<script language="javascript">
  function ShowSelectedGroups() {
    var svals = document.form1.groups.value;
    alert('Groups='+svals);
  }
</script>
<form name="form1">
  <select name="groups" onchange="ShowSelectedGroups();">
    <option value="1">Grok</option>
    <option value="2">Raven</option>
    <option value="3">Wizzbert</option>
  </select>
</form>


But when the SELECT is a multiselect, the value shown is always the top entry.  Thus if you select all 3 entries, it will say "Groups=1".  How do I get it to say "Groups=1,2,3"?

The real question is if select.value returns multiple values, how do I know this, and combine them for display?

Grok

Also tried this -- which still shows only the first selected value:
      function ShowSelectedGroups() {
        var savals = new Array(document.form1.groups.value);
        var svals = savals.toString();
        alert('Groups='+svals);
      }

TehUser


var selectedArray = new Array();
var selectionObject = document.getElementById('groups');
var index = 0;
var i;

for( i = 0; i < selectionObject.options.length; i++ )
{
  if( selectionObject.options[i].selected )
    selectedArray[index++] = selectionObject.options[i].value;
}


Should work or at least give you the idea.

Grok

No, I do not want to loop through all the options to see what is selected.  I have been told that the value property of the select object contains the values that are selected.  Where it is only one, it contains that value, and when it is more than one, it is presented either as a comma-delimited list of values, or an array, or something.  So I guess the best question is how do I determine the datatype of the document.form1.groups.value object?  isArray() does not appear to work for me.

TehUser

typeof() will tell you the type.

The form.select.value is always the value of one selection item.  It does not return an array.

Grok

Is it true that .value is only one item, even when >1 are selected in a multiselect?  If you found the official spec on this, share, I could not locate it on any javascript/DOM sites.


quasi-modo

grok what are you trying to do (big picture)?
WAR EAGLE!
Quote(00:04:08) zdv17: yeah i quit doing that stuff cause it jacked up the power bill too much
(00:04:19) nick is a turtle: Right now im not paying the power bill though
(00:04:33) nick is a turtle: if i had to pay the electric bill
(00:04:47) nick is a turtle: id hibernate when i go to class
(00:04:57) nick is a turtle: or at least when i go to sleep
(00:08:50) zdv17: hibernating in class is cool.. esp. when you leave a drool puddle

UnLeaDeD

Try making another variable that stores the option value numbers. Then use if statements (depending on what your doing) to display objects.