SELECT <option value..>

M

magix

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 ?


thanks.
 
M

magix

magix said:
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 ?


thanks.


and also how can I use document.getElementById('sIdname').value to retrieve
10 and John? (Assume <Select name="sIdName" id="sIdName"..>, some sort of
split function need to be done.
 
E

Evertjan.

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>
============================
 
M

magix

Randy Webb said:
magix said the following on 3/17/2007 5:28 AM:
Hi,

in my SELECT (like below), the option value has 2 values, separated with
comma ","

No, it has one value that contains a 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 ?

var selectValue = document.forms['formName'].elements['selectName'].value
var theArray = selectValue.split(',')
var firstValue = theArray[0]
var secondValue = theArray[1]

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/


Thank you. Case Closed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top