[Javascript] problem getting the text value of a select box
Rochester, Dean
Dean.Rochester at ProMedica.org
Fri Mar 1 14:28:05 CST 2002
Hi
I am trying to write a reusable if statement that I can check the value of a
select box's text, but I do not know the name of the select box. I am
walking throught a forms elements and getting the value. If the element is
a select box, then the standard .value will always be null even if the
select box has text in it. So in the listing below, towards the bottom the
last if statement is where the problem is.
if (strField == null || strField == "") {
If the element is a select box, this will be true and then I need to get the
option[selectedIndex].text but I do not have the name of the select box. I
can get the name of the select box, but then I need to put it into the
following command.
document.forms[0].selectName.options[list.selectedIndex].text
where I want to replace selectName with a variable for the name of the
select box.
Here is what I have tried and it does not work. It tells me that
elements[...] is null
Thanks in advance
Dean-O
-------------------------------------
function editTab(elClass) {
var form1 = document.forms[0];
// elClass is the class attribute for all the elements on the current form
var elemClass = elClass;
var allTags = form1.getElementsByTagName('*');
var elements = new Array();
for (var e = 0; e < allTags.length; e++){
if (allTags[e].className == elemClass)
elements[elements.length] = allTags[e];
}
//get a list of all the tags in the form
for (var intLoop = 0; intLoop < elements.length; intLoop++) {
if (elements[intLoop].className == elemClass) {
// save edit parameters
strEdit = elements[intLoop].getAttribute('edit');
// if edits, perform them
if (strEdit != null) {
strFldName = elements[intLoop].getAttribute('title') + " (" +
elements[intLoop].getAttribute('name') + ")"; // save field name
strField = elements[intLoop].getAttribute('value');
// if the field is a select box the value from the line
above will
// be null even if there is a value in the box.
// you need the option selected value for the strField
variable
if (strField == null || strField == "") {
//var ename = elements[intLoop].getAttribute('name');
var OptionsList =
elements[intLoop].getAttribute('name');
document.writeln("<br>OptionsList = " + OptionsList +
"</br>");
strField =
elements[intLoop].[elements[intLoop].getAttribute('name')].options[OptionsLi
st.selectedIndex].text;
document.writeln("<br>strField = " + strField +
"</br>");
}
}
}
}
}
More information about the Javascript
mailing list