• Post Reply Bookmark Topic Watch Topic
  • New Topic

Javascript: SELECT Object?  RSS feed

 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In myForm have N the same name(mySelectName) SELECT object
How to get value of each selected mySelectName?
-----------------------------
<form name = myForm ...>
....
<select name = "mySelectName">
<option value="1"A</option>
<option value="2"B</option>
</select>
....
<select name = "mySelectName">
<option value="1"A</option>
<option value="2"B</option>
</select>
...
</form>
Please help.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm not sure how that works for multiple selections.

Jules
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be able to do it like the following, but I did not test it:

mySelect = document.myForm.mySelectName[0];
selectedValue1 = mySelect.options[mySelect.selectedIndex].value;

mySelect = document.myForm.mySelectName[1];
selectedValue2 = mySelect.options[mySelect.selectedIndex].value;

If that does not work, then you need to use the form object array

document.forms[0].elements[X]; where X is the integer value of the element in the array.

Eric
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, didn't read the question properly. :roll: I'd be interested to know why you want to give all your SELECTs the same name.

Jules
 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a html where contains list items. When user click Submit button,
javasript function will be called.
In js function, I want get these values to check before applying...

JV.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So give the lists different names; preferably meaningful ones related to their content. Then use the syntax I supplied to access the selected element. I still don't know how to do it if there's more than one selected element in a multi-select. Eric? Anyone?

Jules
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer above is the solution

Eric
 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your help.

I did with different name, but have problems when getting values.
This is my javascript using to get values.

Have a problem with code "form.COUNT.value". I am beginer with js.
How to solve my problem?

Please help if you can.

function getMemberNumbers(form){
var nNum = 0;
for (i = 0; i < 10; i ++) {
var COUNT = "syoAmount" + i;
     nNum += parseInt(form.COUNT.value);
}
return nNum;
}
...
<select name ="syoAmount0">
....
</select>
<select name ="syoAmount1">
....
</select>
...
<select name ="syoAmount9">
....
</select>

JV.
 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

It done!
This is my script.

function getMemberNumbers(form){
var nNum = 0;
for (i = 0; i < 10; i ++) {
var obj = form['syoAmount' + i];
nNum += parseInt(obj.options[obj.selectedIndex].value);
}
return nNum;
}

JV.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd be better off using Eric's solution, i.e.

You may as well give all your selects the same name if they have the same function. Just appending a number to each name is pretty pointless.

I take it they're single-select drop-downs rather than multi-select list boxes.

Jules
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!