Check the value of all combo boxes on a page

S

Stephen Miller

I have a page with many, dynamically generated combo boxes and I want
to check all of them, before I add a unique value to specific combo.
What would be the best way to work through the document and without
needing to know the name of each combo box, check its value?

Thanks,

Stephen
 
T

Thomas 'PointedEars' Lahn

Stephen said:
I have a page with many, dynamically generated combo boxes and I want
to check all of them, before I add a unique value to specific combo.
What would be the best way to work through the document and without
needing to know the name of each combo box, check its value?

There is no such thing like combo boxes in HTML. You probably mean
check boxes:

if (document.getElementsByTagName)
{
var o = document.getElementsByTagName('input');
if (o)
{
for (var i = o.length; i--; 0)
{
if (o.type = "checkbox")
{
o.checked = true;
}
}
}
}

What do users without support for client-side JS?


PointedEars
 
K

kaeli

I have a page with many, dynamically generated combo boxes

You mean select elements?
Combo boxes are VB and allow both a selection and text input.
and I want
to check all of them, before I add a unique value to specific combo.
What would be the best way to work through the document and without
needing to know the name of each combo box, check its value?

Please explain what you mean by "check its value".
Select elements have options. They also have a selectedIndex property to tell
which option has been chosen. The options have values.
Do you want to check what options (option values) are in each select, which
option has been chosen for a select...?

To iterate through a form, named "form1":

var L = document.forms["form1"].elements.length;
for (var i=0; i<L; i++)
{
/* 'e' will be a form element; do whatever with it */
var e = document.forms["form1"].elements;
}

'e' will have a type, name, and all other attributes associated with a form
element.
Example:
alert(e.name);
alert(e.type);

I'm assuming the elements are in a form. The above is more cross-browser than
using getElementsByTagName, but I think it's also slower. Using
getElementsByTagName, however, will allow you to get all the select elements
across ALL forms in a page if you have multiple forms, not just one. There's
pros and cons to each choice.
And don't forget - not everyone has javascript enabled or even available.

HTH
--
 

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
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top