[thelist] [DHTML] Elements with the same ID

David Dorward evolt at david.us-lot.org
Wed Mar 19 00:33:58 CST 2003


On Wed, Mar 19, 2003 at 03:47:28 +1100, Mark M wrote:
> If you have multiple elements that have the same id in a page.. 

HTML does not allow you to have multiple elements that have the same
id in a page. The class attribute is used for marking up elements as
being part of a group.

> can you access them all through the same ID?
> or does the system automatically create an array you can loop through under that ID?
> The reason I ask is because we have a series of images on a page that we want to 
> hide/display on a single click... if we give them all the same ID it makes life easy.

<img ... class="extra"> (or whatever class name you use) with a script along the lines of this:

function flip(e) {
    if (document.getElementById(e).style.display == 'none') {
        document.getElementById(e).style.display = '';
    } else {
        document.getElementById(e).style.display = 'none';
    }
}

function hideall(hiddenContentElement,hiddenContentClass) {
	for (i=0;i<document.getElementsByTagName(hiddenContentElement).length; i++) {
		if (document.getElementsByTagName(hiddenContentElement).item(i).className == hiddenContentClass){
			document.getElementsByTagName(hiddenContentElement).item(i).style.display = "none";
		}
	}
}

window.onload = function(){
    hideall("img","extra");
};

-- 
David Dorward                                   http://david.us-lot.org/
"You cannot rewrite history, not one line."
                                      - The Doctor (Dr. Who: The Aztecs)


More information about the thelist mailing list