473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multiple select elements with same name

Hello,

There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
....
}
else {
....
}

But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?

Thanks in advance.

Sam

May 11 '07 #1
17 16954
Lee
Sam Kong said:
>
Hello,

There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...
}
else {
...
}

But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?
I think the usual thing to do would be to change one of the names.
--

May 11 '07 #2
Daz
On May 11, 7:31 pm, Sam Kong <sam.s.k...@gmail.comwrote:
Hello,

There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...}

else {
...

}

But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.

How do you usually handle this problem?

Thanks in advance.

Sam
var elements = document.getElementsByName("select1");

This returns an array of elements with the specified name.

You can then iterate through it if you want, and do stuff with each,
or just use "length" to see how many there are.

alert(elements.length);

Lee's proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don't have to be unique), I don't see any
point in having more than one element with the same name. You will
most likely find yourself running into a lot of problems.

I find it good practice to give anything that needs to be identified a
name and an id. The id and the name should match, thus giving you a
unique name for everything that needs one. Obviously, if it's
something that doesn't need an id, but only a name, or vice-versa,
then don't give it one. But if you give one element an id of
"select1", even if you don't give it a name, I would consider
"select1" to be out of use for any other element names. You will find
that coding like this helps you understand things and saves you a lot
of time fixing problems.

All the best.

Daz.

May 11 '07 #3
On May 11, 12:20 pm, Daz <cutenfu...@gmail.comwrote:
On May 11, 7:31 pm, Sam Kong <sam.s.k...@gmail.comwrote:
Hello,
There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].
To check if there are more than one, I thought I can use
if (form1.select1.length) {
...}
else {
...
}
But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.
How do you usually handle this problem?
Thanks in advance.
Sam

var elements = document.getElementsByName("select1");

This returns an array of elements with the specified name.

You can then iterate through it if you want, and do stuff with each,
or just use "length" to see how many there are.

alert(elements.length);

Lee's proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don't have to be unique), I don't see any
point in having more than one element with the same name. You will
most likely find yourself running into a lot of problems.

I find it good practice to give anything that needs to be identified a
name and an id. The id and the name should match, thus giving you a
unique name for everything that needs one. Obviously, if it's
something that doesn't need an id, but only a name, or vice-versa,
then don't give it one. But if you give one element an id of
"select1", even if you don't give it a name, I would consider
"select1" to be out of use for any other element names. You will find
that coding like this helps you understand things and saves you a lot
of time fixing problems.

All the best.

Daz.
Daz,

Thank you for the kind answer.
I think you provided the right solution and good advice.

Sam

May 11 '07 #4
ASM
Sam Kong a écrit :
Hello,

There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...
}
else {
...
}

But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.
if (form1.select1 &&
form1.select1.length != form1.select1.options.length) {
alert(form1.select1.length+' select1');
}
else
if (form1.select1 &&
form1.select1.length == form1.select1.options.length) {
alert('only one select1');
}
else
alert('no select1');
How do you usually handle this problem?
I do not give same name to my elements ...

or

if(
document.getElementsByName('select1') &&
document.getElementsByName('select1').length >=1
)
alert(document.getElementsByName('select1').length +' select1')
or for elements of a particular form :

if(
document.form1.getElementsByName('select1') &&
document.form1.getElementsByName('select1').length >=1
)
alert(document.form1.getElementsByName('select1'). length+' select1')
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
May 11 '07 #5
On May 11, 1:20 pm, Daz <cutenfu...@gmail.comwrote:
Lee's proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don't have to be unique), I don't see any
point in having more than one element with the same name.
Have you done much web development?

Having multiple inputs of the same name is not only extremely useful,
but perfectly allowable by all standards. If it causes any minor
javascript annoyances, those can be easily worked around.

Matt Kruse
May 11 '07 #6

ASM wrote:
Sam Kong a écrit :
Hello,

There are 1 or more select elements with the same name in a form.
Let's say that the name of the select is 'select1' and the name of the
form is 'form1'.
If there's only one select I access it like form1.select1.
If there are more than one select, I access them like
form1.select1[index].

To check if there are more than one, I thought I can use
if (form1.select1.length) {
...
}
else {
...
}

But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.

if (form1.select1 &&
form1.select1.length != form1.select1.options.length) {
alert(form1.select1.length+' select1');
}
else
if (form1.select1 &&
form1.select1.length == form1.select1.options.length) {
alert('only one select1');
}

or
var select=form1.select1;
if(select.length>1)
alert("2 or more selects")
else
alert("only one")

How do you usually handle this problem?
if(
document.getElementsByName('select1') &&
document.getElementsByName('select1').length >=1
)
There is no need to check getElementsByTagName(xx) because it will
always return a nodelist (==true) if the function is supported.
to check for function, use:

if(
document.getElementsByName &&
document.getElementsByName('select1').length >=1
)

May 11 '07 #7
ASM
scripts.contact a écrit :
>
or
var select=form1.select1;
if(select.length>1)
alert("2 or more selects")
else
alert("only one")
No, because as said :
>>But that doesn't work because if there's only one select,
form1.select1.length is a valid property which is the length of
options.
and :
else alert("only one")
could fire if there is NO select1
>if(
document.getElementsByName('select1') &&
document.getElementsByName('select1').length >=1
)

There is no need to check getElementsByTagName(xx) because it will
always return a nodelist (==true)
Oooops !
if the function is supported.
to check for function, use:

if(
document.getElementsByName &&
document.getElementsByName('select1').length >=1
)

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
May 12 '07 #8
ASM wrote:
var select=form1.select1;
if(select.length>1)
alert("2 or more selects")
else
alert("only one")

No, because as said :
>>But that doesn't work because if there's only one select,
>>form1.select1.length is a valid property which is the length of
>>options.

and :
else alert("only one")
could fire if there is NO select1
Oooops!

May 12 '07 #9
On May 12, 10:21 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
ASM wrote:
var select=form1.select1;
if(select.length>1)
The real "oooops" here is that checking the length property is a silly
way of trying to distinguish between a select element and a NodeList -
it is the only property that the two have in common. It is also
likely that the length will be the same for both - either two selects
or one select with two options will return an object with a length of
two.

I think a better strategy is to check for the name property - a
NodeList doesn't have one, the select element must (in this case) so:

var select = document.forms['form1'].elements['select1'];
if (select.name == 'select1') {
/* select is a select element */
} else {
/* select is a NodeList */
}
--
Rob

May 13 '07 #10
ASM
RobG a écrit :
On May 12, 10:21 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
>ASM wrote:
>>>var select=form1.select1;
if(select.length>1)
[...]
I think a better strategy is to check for the name property - a
NodeList doesn't have one, the select element must (in this case) so:

var select = document.forms['form1'].elements['select1'];
if (select.name == 'select1') {
/* select is a select element */
} else {
/* select is a NodeList */
}
Not too bad :-)

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
May 13 '07 #11
Daz
On May 11, 10:46 pm, Matt Kruse <m...@mattkruse.comwrote:
On May 11, 1:20 pm, Daz <cutenfu...@gmail.comwrote:
Lee's proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don't have to be unique), I don't see any
point in having more than one element with the same name.

Have you done much web development?

Having multiple inputs of the same name is not only extremely useful,
but perfectly allowable by all standards. If it causes any minor
javascript annoyances, those can be easily worked around.

Matt Kruse
No, I am fairly new to Web development. I know it's allowed to have
multiple names by all standards, I just don't see much of a point to
it.

Please could you post an example of where having identically named
elements would be "extremely useful"? I've never had the need to use
them, hence why I don't see the point. Perhaps if the need arose, then
of course, I would. From what the OP posted, I can't see any reason
why he should need the HTML to remain the way it is.

Thanks.

May 17 '07 #12
Daz said the following on 5/17/2007 12:02 PM:
On May 11, 10:46 pm, Matt Kruse <m...@mattkruse.comwrote:
>On May 11, 1:20 pm, Daz <cutenfu...@gmail.comwrote:
>>Lee's proposed solution is not a bad one. Whilst names are not like
ids (in the sense that they don't have to be unique), I don't see any
point in having more than one element with the same name.
Have you done much web development?

Having multiple inputs of the same name is not only extremely useful,
but perfectly allowable by all standards. If it causes any minor
javascript annoyances, those can be easily worked around.

Matt Kruse

No, I am fairly new to Web development. I know it's allowed to have
multiple names by all standards, I just don't see much of a point to
it.

Please could you post an example of where having identically named
elements would be "extremely useful"?
Radio Buttons or Checkboxes are the first two that come to mind. Other
than those two, inputs with the same name - in the same form - are more
of a headache than they are worth. And it makes them impossible to use
the forms collection to get a reference to them using the name attribute
(other than RB and CB'es).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 17 '07 #13
On May 17, 11:19 am, Randy Webb <HikksNotAtH...@aol.comwrote:
Daz said the following on 5/17/2007 12:02 PM:
Please could you post an example of where having identically named
elements would be "extremely useful"?
On 'grid' forms with a variable number of rows containing identical
inputs in each column.
Each input in each column is given the same name, and the values are
just treated as arrays when they get to the server. That way if you
want the "name" field from row 10, you just look at name[9] (or
whatever syntax, given your language of choice). The alternative is to
use a name like name_10 with the index in the name itself, but that is
far more hassle than it's worth. Plus, when you dynamically (client-
side) add new rows to be edited you can just copy a template row and
you don't have to worry about renaming inputs, etc.
And it makes them impossible to use
the forms collection to get a reference to them using the name attribute
(other than RB and CB'es).
Why?

document.forms['myform'].elements['name'][9] gives you the 10th input
with the name "name".

You're just used to it with radio buttons and checkboxes so it feels
natural with those and not with 'normal' inputs.

Matt Kruse

May 18 '07 #14
On May 18, 2:02 am, Daz <cutenfu...@gmail.comwrote:
[...]
Please could you post an example of where having identically named
elements would be "extremely useful"?
Whenever an attribute or property can have multiple values - e.g. an
element's class attribute, or where you might use a multiple select
but want to use two (or three or four) separate selects.
--
Rob

May 18 '07 #15
Matt Kruse said the following on 5/17/2007 10:33 PM:
On May 17, 11:19 am, Randy Webb <HikksNotAtH...@aol.comwrote:
<snip>
>And it makes them impossible to use
the forms collection to get a reference to them using the name attribute
(other than RB and CB'es).

Why?

document.forms['myform'].elements['name'][9] gives you the 10th input
with the name "name".
I had something else in mind when I wrote it. Hadn't really thought
about the way you did it as I have never had a reason to do it :)
You're just used to it with radio buttons and checkboxes so it feels
natural with those and not with 'normal' inputs.
Probably true :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '07 #16
Daz
On May 18, 3:33 am, Matt Kruse <m...@mattkruse.comwrote:
>
On 'grid' forms with a variable number of rows containing identical
inputs in each column.
Each input in each column is given the same name, and the values are
just treated as arrays when they get to the server. That way if you
want the "name" field from row 10, you just look at name[9] (or
whatever syntax, given your language of choice). The alternative is to
use a name like name_10 with the index in the name itself, but that is
far more hassle than it's worth. Plus, when you dynamically (client-
side) add new rows to be edited you can just copy a template row and
you don't have to worry about renaming inputs, etc.
Aaaah, yes, of course! I had read about that before, but never
actually made use of it. I think that's the solution to a problem I
experienced with one of my first projects. Thanks for setting me
straight.
May 19 '07 #17
Daz
On May 19, 9:32 am, Randy Webb <HikksNotAtH...@aol.comwrote:
>
I had something else in mind when I wrote it. Hadn't really thought
about the way you did it as I have never had a reason to do it :)
Same here! Hehe.
May 19 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
12
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
3
by: d.schulz81 | last post by:
Hi all how can i manipulate a multiple select into a single select dropdown field with JavaScript? thanks
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
2
by: rehevkor5 | last post by:
I have written Javascript which populates a form with <select> elements dynamically based on some other stuff going on in the page. The problem is that I can't get it to work properly in IE, and...
5
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
2
by: dinkle | last post by:
Hi Y'all, I am pretty new to js and am hitting a few snags. I need to process a multiple select list and pass it onto a PHP script. I can only get the first value in the JS and have no idea how...
2
by: helplakshmi | last post by:
Hi All, I am new to php. The form that i am designing has few input input fields with submit and reset button. The functionality of submit and reset are working properly till now. My form ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.