View Full Version : hide elements based on ID

10-13-2005, 04:12 PM
I've got the following function which works perfectly...

for (x=1;x<howMany;x++){
what = "b_" + whichProcess + "_" + x;
document.getElementById(what).style.display = '';

the function will pass how many elements there are to be changed along with their ID numbers... it's used to hide rows on tables... eg:

<tr id="b_1_1"></tr>
<tr id="b_1_2"></tr>
<tr id="b_1_3"></tr>

the above hide perfectly... but I have other rows such as

<tr id="b_1_1"></tr>
<tr id="b_1_2"></tr>
<tr id="b_1_3"></tr>
<tr id="b_2_1"></tr>
<tr id="b_2_2"></tr>
<tr id="b_3_1"></tr>

my question is how can I make a loop which will hide ALL elements which begin with "b_"

thanks for any help!

10-13-2005, 04:25 PM
function hideEm() {
var els = document.getElementsByTagName("*");
for(var i=0; els[i]; i++) {
if(/^b_/i.test(els[i].getAttribute("id"))) {
with(els[i].style) {
visibility = "hidden";
// modify other element styles here
// e.g:
//display = "none";
}Hope this helps


10-14-2005, 09:50 AM
excellent!!! Works perfectly! - thankyou very much!! :)