467,182 Members | 966 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,182 developers. It's quick & easy.

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

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
  • viewed: 1497
Share:
2 Replies
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
"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.

Similar topics

5 posts views Thread by Brian Foley | last post: by
7 posts views Thread by lambertb@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.