First off, thank you very much for both your contributions. I knew it was a weird way to code but I wanted to eliminate some redundant looping and reduce the loop rate to the actual elements, which would in turn build an array.
The original function went something like this.
Code:
var tagsarray = new Array();
function defineTotal() {
for(var i=1;document.getElementById('retreat' + i);i++) // count all instances of the select form field
{
if (document.getElementById('retreat'+[i])) {
tagsarray.push(document.getElementById('retreat'+[i])) // list all instances in an array
}
}
alert(tagsarray[0].options[tagsarray[0].selectedIndex].value);
}
I suppose the conventional way to do this is written below and I eventually used this.
Code:
var tagsarray = new Array();
function defineTotal() {
tag = document.getElementsByTagName('select');
for(var i=0;i<tag.length;i++) // count all instances of the select form field
{
if (document.getElementById('retreat'+[i])) {
tagsarray.push(document.getElementById('retreat'+[i])) // list all instances in an array
}
}
alert(tagsarray[0].options[tagsarray[0].selectedIndex].value;
}
But, after I came back to this forum and read the posts, I removed the if statement from the for loop's statements (otherwise I guess I would have created a subroutine). And I reverted from the conventional way to my original way because it removed the need to cycle through with getElementsByTagName().
The point of the exercise, in my mind, was to loop only through direct instances of the tag controlled by an incremental id (i.e. retreat1, retreat2).
I know it's clunky, and I am now having trouble looking for a good way to obtain the value of the selected index and to use this value in some eCommerce kind of fashion to calculate a total.
Bookmarks