|
-
Sep 2nd, 2004, 11:00 AM
#1
Thread Starter
Frenzied Member
getelementbyid for group? [Resolved]
Do I have to specify a different ID for each item... or can I do something with a group of items?
For example, I have a group of textboxes that need to be visible or invisible on the click of a button. If I give them all the same ID, why won't it perform the action on all of those items?
Last edited by ober0330; Sep 2nd, 2004 at 02:58 PM.
-
Sep 2nd, 2004, 11:09 AM
#2
By the SGML, HTML and XML standards, giving more than one element the same ID is highly invalid.
I suggest you give the textboxes an ID pattern (e.g. txtbox1, txtbox2, ...) and use a short loop to make all of them invisible:
Code:
var o, i;
for(i = 1; (o = document.getElementById("txtbox"+i)) != null; ++i) {
o.style.visibility = "hidden";
}
As long as there are no gaps, this will run through all the textboxes that use the same pattern.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 2nd, 2004, 11:15 AM
#3
Frenzied Member
if the pattern starts at 1, then I think you'll want to starts off with i = 0, not i = 1. I might be wrong, just try it out.
Have I helped you? Please Rate my posts. 
-
Sep 2nd, 2004, 11:21 AM
#4
Frenzied Member
If you need more than one element with the same id, use classes then.
http://www.webmasterworld.com/forum91/1729.htm
Look at the bottom, there's a unofficial getElementByClass function.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Sep 2nd, 2004, 11:24 AM
#5
Frenzied Member
I need to log in (which I can't) to see the post. Using unofficial functions isn't very good though, it's quite likely not to work in all real browsers.
Could you either paste the post from the other forum into here or could you yourself give a brief description.
Edit: never mind. I found the same post on google and it didn't ask me to log in.
Have I helped you? Please Rate my posts. 
-
Sep 2nd, 2004, 11:28 AM
#6
Frenzied Member
Here's the post which I think you were talking about. It's quite useful, but I prefer CB's method, this can be used other times though.
In the process of developing a web based application UI I had to devise a method that would allow us to manipulate objects of a certain class. I didn't want to be constrained by using the ID, as I wanted flexibility in the system. I didn't want to have to worry about what objects were on the which page for the script to know about.
Unfortunately, there is no getelementbyclass, so I wrote one myself. For anyone interested here it is for you to use and abuse:
//Create an array
var allPageTags = new Array();
function doSomethingWithClasses(theClass) {
//Populate the array with all the page tags
var allPageTags=document.getElementsByTagName("*");
//Cycle through the tags using a for loop
for (i=0; i<allPageTags.length; i++) {
//Pick out the tags with our class name
if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
}
}
}
The advatage for me was that I didn't need to worry about whether an ID was on a page or not. Scripts that manipulate elements usually work on the basis of the ID. If the ID is not part of the DOM (not on the page) the script will throw an error. Using a class name will manipulate every object which is of that class and as developers we don't need to worry about the presence of the ID or not.
I look forward to all comments and criticisms... But mainly the comments!
Have I helped you? Please Rate my posts. 
-
Sep 2nd, 2004, 02:57 PM
#7
Thread Starter
Frenzied Member
Thanks guys... that's perfect as usual.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|