arvanhalleorg
asked on
array elements getElementById
Okay maybe this is easy and I'm just entirely missing the concept, but since I urgently need it and can't figure it out, it's a 500.
I know that we can do this :
document.frmName.eleName[# ].value;
but is there a way to get this with getElementById?
I have an array of elements named HR for regular hours, but i'd like to get their values through a loop to calculate totals.
I tried doing document.getElementById('H R')[#].val ue but it gives an error.
Thanks!
I know that we can do this :
document.frmName.eleName[#
but is there a way to get this with getElementById?
I have an array of elements named HR for regular hours, but i'd like to get their values through a loop to calculate totals.
I tried doing document.getElementById('H
Thanks!
The method getElementById() will return only the First element with that id.
But what is the problem to get first the collection of all elements with the given name?
var elemColl = document.frmName.eleName;
Then is the twentyfirst element accessed like this:
elemColl[21].value = "some value";
But what is the problem to get first the collection of all elements with the given name?
var elemColl = document.frmName.eleName;
Then is the twentyfirst element accessed like this:
elemColl[21].value = "some value";
You might also consider using
var tables = document.getElementsByTagN ame( 'table' );
This will be a collection (array) of references to every table on your page, the first of which could be manipulated using something like:
var table = tables[ 0 ];
Now that you have a reference to a particular table, you can locate the individual 'tr' elements in that table using:
var rows = table.getElementsByTagName ( 'tr' );
Since the method was executed using the specific "table" reference, the result will be a collection of the row references,
but only for this specific table.
var tables = document.getElementsByTagN
This will be a collection (array) of references to every table on your page, the first of which could be manipulated using something like:
var table = tables[ 0 ];
Now that you have a reference to a particular table, you can locate the individual 'tr' elements in that table using:
var rows = table.getElementsByTagName
Since the method was executed using the specific "table" reference, the result will be a collection of the row references,
but only for this specific table.
ASKER
well i wanted to use document.form.element.yada yada but can I do this without the document.form part? I refuse to put a form on my page cause frankly i'm not going to submit the thing, it's only the javascript that is taking the data and using xmlHttp.Send to get the asp to make the changes in the background. I was wanting to use getElementById for that reason, cause it doesnt ask me for the form.
So, does the following work?
HR[#].value;
or do I absolutely have to use the full syntax?
So, does the following work?
HR[#].value;
or do I absolutely have to use the full syntax?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sweet! That's perfect. :D Thank you!
Since a tag's id should be unique on that page getElementById just returns that tag. It would not work in this case and your page may have other issues with duplicating ids. I recommend that you look at getElementsByTagName. That will return a "collection" that you could loop through to find the exact ones you want or, if they are the only ones with that tag, then it will not need the loop.
Let me know if you have any questions or need more information. If you need more details then please provide the relevant code you are using.
b0lsc0tt