Solved

array elements getElementById

Posted on 2006-10-26
6
8,234 Views
Last Modified: 2013-11-19
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('HR')[#].value but it gives an error.

Thanks!
0
Comment
Question by:arvanhalleorg
6 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17815029
arvanhalleorg,

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
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17815162
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";


0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17816520
You might also consider using

  var tables = document.getElementsByTagName( '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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:arvanhalleorg
ID: 17816544
well i wanted to use document.form.element.yadayada 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?
0
 
LVL 3

Accepted Solution

by:
jabaswavika earned 500 total points
ID: 17817601
You should use document.getElementsByName method. This would give you all the elements with that name

Use (document.getElementsByName('HR'))[#].value
    to get the value of your HR element. Note that the NAME, I repeat the NAME should be "HR". rather than the tagname or the id for the method to work.

See the test code below

<html>
<body>
<input type="text" name="test"/><br/>
<input type="text" name="test"/><br/>
<input type="text" name="test"/><br/>
<input type="button" onClick="testing();">
<script>
function testing(){
alert((document.getElementsByName("test"))[0].value);
alert((document.getElementsByName("test"))[1].value);
alert((document.getElementsByName("test"))[2].value);
}
</script>
</body>
</html>
</script>
</body>
</html>
0
 
LVL 2

Author Comment

by:arvanhalleorg
ID: 17818147
Sweet! That's perfect. :D Thank you!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now