var obj = document.getElementById('form_id');
var myArray = new Array();
var _input = obj.getElementsByTagName('input');
var _select = obj.getElementsByTagName('select');
myArray = _input.concat(_select);
I'm trying to get an array that contains all the input & select tags. When I run this, myArray contains the input tags, but not the select tags.
Any suggestions would be greatly appreciated.
Brian
I tried putting an alert like you suggested, and I'm getting an error that _input.join is not a function. Which tells me that _input is not an array. But when I do "alert(_input.length)" I get an alert that says "2". So is a list of objects not an array? I didn't realize that a list is different that an array. Is there a way to join two lists together?
var myArray = new Array();
for (i in _input) { myArray[myArray.length] = i; }
for (i in _select) { myArray[myArray.length] = i; }
alert(myArray.join('\n'));
LOL.. I tried your code, and I can't figure out what it's outputting either.
I tried substituting "alert(myArray[i].name)" for "alert(myArray.join)". I got undefined for each item. The other weird thing is that if I do "alert(myArray.length)" it says there are 14 items in the array, but there should be 8....
So using your code, I modified the function to this, and it seems to work:
Code:
function DisplayTags() {
var obj = document.getElementById('form_id');
var _input = obj.getElementsByTagName('input');
var _select = obj.getElementsByTagName('select');
var myArray = new Array();
for(var i=0; i<_input.length; i++){ myArray[myArray.length] = _input[i];}
for(i=0; i<_select.length; i++){ myArray[myArray.length] = _select[i]; }
for(i=0; i<myArray.length; i++){
alert("name: "+myArray[i].name+"\nitem "+(i+1)+" of "+myArray.length);
}
}
It's the same basic idea, but for some reason, when addressing the array with the index value, it seems to work.
Thanks for your help. I was hoping for a cleaner solution, like using a method of the Array object, but I can't seem to figure it out.
You're most welcome.
I guess we'll need the help of some more experienced forum members to explain what's happening ,
but so long as it works for you, I'm happy ...
Bookmarks