Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    hide elements based on ID

    hi..
    I've got the following function which works perfectly...

    Code:
    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!

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    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

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    excellent!!! Works perfectly! - thankyou very much!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •