magix wrote on 17 mrt 2007 in comp.lang.javascript
:
Hi,
in my SELECT (like below), the option value has 2 values, separated
with comma ","
<select name="sIdName">
<option value="10,John">John</option>
<option value="11,Eva">Eva</option>
<option value="22,Danny">Danny</option>
</select>
How can I read these two values individually from Javascript statement
? like one is 10, another one is John
i.e document.form.option[document.form.selectedIndex].value ?
It pays to read the specs:
selectedIndex is no child of form but of the select.
form is not the same as forms[0].
use:
============================
<form>
<select name="sIdName">
<option value="10,John">John</option>
<option value="11,Eva" selected>Eva</option>
<option value="22,Danny">Danny</option>
</select>
</form>
<script type='text/javascript'>
var r = document.forms[0].elements['sIdName']
var rr = r.options[r.selectedIndex].value.split(',');
alert(rr[0]); // 11
alert(rr[1]); // Eva
</script>
============================
or:
============================
<select name="sIdName" onchange='chd(this)'>
<option value="--,--">-- ? --</option>
<option value="10,John">John</option>
<option value="11,Eva">Eva</option>
<option value="22,Danny">Danny</option>
</select>
<script type='text/javascript'>
function chd(r) {
if (r.selectedIndex==0) return;
var rr = r.options[r.selectedIndex].value.split(',');
alert(rr[0]);
alert(rr[1]);
};
</script>
============================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)