Will give it a try
Main Topics
Browse All TopicsI have html numerous html elements on a web page with the same id and I want to hide all of these elements using javascript. At the moment the code I have hides one of the elements but leaves the others visible. I want to know is there a way I can hide them all. Here's what I have at the moment:
document.getElementById("i
(Note: id1 is the id for the elements I want to hide)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head>
<script>
function hide( id ) {
var els = getElementsById( id );
for ( var i = 0; i < els.length; i++ ) {
els[i].style.display = 'none';
}
}
function getElementsById( sId ) {
var outArray = new Array();
if(typeof(sId)!='string' || !sId) {
return outArray;
};
if(document.evaluate) {
var xpathString = "//*[@id='" + sId.toString() + "']"
var xpathResult = document.evaluate(xpathStr
while ((outArray[outArray.length
outArray.pop();
} else if(document.all) {
for(var i=0,j=document.all[sId].le
outArray[i] = document.all[sId][i];}
}else if(document.getElementsByT
var aEl = document.getElementsByTagN
for(var i=0,j=aEl.length;i<j;i+=1)
if(aEl[i].id == sId ) {
outArray.push(aEl[i]);
};
};
};
return outArray;
}
</script>
</head>
<body>
<form>
<input type='text' id='t' /><br />
<input type='text' id='t' /><br />
<input type='text' id='t' /><br />
<input type='text' id='t' /><br />
<input type='text' id='t' /><br />
<input type='button' value='Hide' onClick="hide( 't' );" />
</form>
</body>
</html>
Here is one dom approach:
<html>
<head>
<title>Script Demo Gops</title>
<script language="JavaScript">
function hideShow(){
var bd=document.getElementsByT
for(var i=0;i<bd.childNodes.length
if(bd.childNodes[i].id=="i
bd.childNodes[i].style.dis
}
}
}
</script>
</head>
<body style="font-family:verdana
<input type="text" id="id1" value=""><br id="id1">
<input type="text" id="id1" value=""><br id="id1">
<input type="text" id="id1" value=""><br id="id1">
<input type="text" id="id1" value=""><br id="id1">
<select>
<option>A
<option>B
<option>C
<option>D
</select><br>
<input type="text" id="test" value="This will not be hidden"><br>
<input type="text" id="test" value="This will not be hidden"><br>
<input type="text" id="test" value="This will not be hidden"><br>
<select id="id1">
<option>This will be hidden
<option>B
<option>C
<option>D
</select><br id="id1">
<select>
<option>A
<option>B
<option>C
<option>D
</select><br><br>
<input type="button" value="Hide" onClick="hideShow();">
</body>
</html>
Technically, you CAN have more than one element with same ID. Practically, you SHOULD ALWAYS have unique IDs on elements. However, I know of no browser that enforces this policy.
In many cases, a person trying to fix the problem cannot change the HTML, so they are forced to find a way to deal with it.
I agree that it is bad practice, and can cause problems down the line (case in point- this question).
getElementByClassName is not a built in function There are a lot of versions floating around the web. Here is one: http://binnyva.blogspot.co
mark-b, I don't know what you mean when you say "Technically, you CAN have more than one element with same ID", but HTML specifications are quite clear on this subject.
"id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document."
http://www.w3.org/TR/html4
Any idea how I would use the results of calling the function below to make all the elements with the class name passed invisible?
//Get all the elements of the given classname of the given tag.
function getElementsByClassName(cla
if(!tag) tag = "*";
var anchs = document.getElementsByTagN
var total_anchs = anchs.length;
var regexp = new RegExp('\\b' + classname + '\\b');
var class_items = new Array()
for(var i=0;i<total_anchs;i++) { //Go thru all the links seaching for the class name
var this_item = anchs[i];
if(regexp.test(this_item.c
class_items.push(this_item
}
}
return class_items;
}
Are you saying that the HTML I posted in my solution doesn't work? That my browser should have blown up or at least refused to work?
I beg to differ. I can be done, and it IS done. It is technically possible to create an HTML pge where element ids are repeated, and it will work.
If your concerned about validation, then the page we are currently on does not validate either (in fact, there are over 250 problems with it): http://validator.w3.org/ch
It does not mean it doesn't exist. It just means it is not valid.
Well Harri, what can I say?
Maybe you should put in a suggestion to EE to have all the experts who recommend solutions 'that are not valid' to be kicked off this site? That should go over well.
That being said, I think your advice was good (and it worked for him). My real problem was with the condescending elitist statement:
"Guys, really! Think what ID means! YOU CAN'T HAVE TWO ELEMENTS WITH THE SAME ID WITHIN ONE PAGE! Validate your markup, that will show your mistakes."
Perhaps a better way to convey your intolerance in the future might look something like this:
"While those solutions will work for this particular issue, the real problem is that you have duplicate ids in your elements, which is invalid markup and violates W3 standards."
Business Accounts
Answer for Membership
by: HarriPaavolaPosted on 2007-02-20 at 04:37:22ID: 18569812
Two or more elements can't have the same id, that's the whole idea of id. It's something that you can identify a certain element. So change your id's to classes and use getElemtentByClassName