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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing style of elements

    I need to toggle the 'display' style of a certain element, which I have obtained through getElementsByTagName(). I am finding it very difficult compared to if I were using getElementById(). What is the best way to do this?

    Code:
    function Toggle() {
    tables=document.getElementsByTagName('table');
    tables[0].style.display='none'; //Doesn't work
    }

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure you're trying to hide the right table? The code you listed should hide the first table on the page.

    Are you getting any errors?

  • #3
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,696
    Thanks
    0
    Thanked 232 Times in 227 Posts
    Hi there benforbes,

    here is a working example...
    Code:
    <script type="text/javascript">
    <!--
    function Toggle() {
      tables=document.getElementsByTagName('table');
      tables[0].style.display='none'; 
     }
    //-->
    </script>
    
    
    
    <table><tr>
    <td>table 0</td>
    </tr></table>
    <table><tr>
    <td>table 1</td>
    </tr></table>
    <a href="#" onclick="Toggle()">toggle</a>
    coothead

  • #4
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works now for some reason. I don't have the exact code I was originally using, I've since replaced it with another technique. I think I may have been accessing the node collection returned by getElementsByTagName() instead of the element array, if that is even possible. Whatever the case, it's fixed now, thanks.


  •  

    Posting Permissions

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