By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

How to get the <select> element in such way...

P: n/a
abs
Hello everybody.

A piece of html code:

<form ... >
<select ....>
<option ....
<option ....
</select>
<select ....>
<option ....
<option ....
</select>
<select ....>
<option ....
<option ....
</select>
</form>

Is there any way to get to a select element by the order it apperas in the
code ? For example, I'd like to get the value of first option of second
<select> element. I've tried document.select[1].options[0].value but it
doesn't work. Is thare any other, correct way to do it ?

Best regards,
ABS
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
abs wrote:
Is there any way to get to a select element by the order it apperas
in the code ? For example, I'd like to get the value of first option
of second <select> element.


I don't think there is any need to do something like this, since you
can give your select elements names and IDs. If you want to do so
anyway, you could use something like

function getValue(nForm, nSelect, nOption) {
var oForm, i, nElems, oElem, sValue, oOption;
if (!isNaN(nForm)) {
oForm = document.forms[nForm];
if (oForm) {
for (nElems=oForm.length, i=0; i<nElems; i++) {
oElem = oForm.elements[i];
if (oElem.type.indexOf("select") == 0) {
nSelect--;
if (nSelect < 0) {
oOption = oElem.options[nOption];
sValue = oOption && oOption.value;
break;
}
}
}
}
}
return sValue;
}

getValue(2,1,4) retrieves the value of the fifth option of the second
select control in the third form in the document. If there are less
options or select controls or forms than requested, getValue() returns
undefined.

ciao, dhgm
Jul 23 '05 #2

P: n/a
"abs" <ni*******@wp.pl> skrev i meddelandet
news:d1**********@inews.gazeta.pl...
Hello everybody. <snip> Is there any way to get to a select element by the order it apperas in the
code ? For example, I'd like to get the value of first option of second
<select> element. I've tried document.select[1].options[0].value but it
doesn't work. Is thare any other, correct way to do it ?


getElementsByTagName("select") will return an array of <select> elements in
source code order.

--
Joakim Braun
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.