Can I not use getElementsByTagName to loop through and grab the value of all the SELECT boxes?
I've been using this code below, which works fine, but if the SELECT is wrapped in a DIV, it doesn't work, so I thought getElementsByTagName would get around that.
Code:
if (obj.childNodes[i].tagName == "SELECT") {
var sel = obj.childNodes[i];
getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
}
I'm just trying to understand how the code below should work. I see that it checks if the tag name is SELECT and then grabs the selectd value, and then builds the string. I keep getting an error obj.getElementsByTagName("select")[i] is undefined
Code:
if (obj.getElementsByTagName("select")[i].tagName == "SELECT") {
var sel = obj.getElementsByTagName("select")[i];
getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
}
I only have one select box so I suppose I could just grab the value with getElementById, but it would be nice to get the above working for future Ajax forms.
Does anyone have any other example Ajax scripts that build a GET/POST string for forms? The one above was all I could find.
What I was trying to explain is that you can't use this line in the existing "FOR" loop:
if (obj.getElementsByTagName("select")[i].tagName == "SELECT") {
Your FOR loop started looking through all "INPUT" tags, and it will *never* find a "SELECT" tag in that collection. Then you took the unrelated index (i) in a collection of "SELECT" tags which only has the one "SELECT" object, so you get "undefined".
You would need to add a separate "FOR" loop (in addition to the existing loop) to search for "SELECT" objects.
Also, there is no reason to create an array of tagName "SELECT" objects and filter any further. They will *all* have a tagName of "SELECT".
Bookmarks