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
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Arrow Help with simple script

    Hi,

    I'm trying to fix a script that does a search for all links on a page and applies a display:none style to each one.

    Here's what I have so far:

    Code:
    <html>
    
    <head>
    
    <title>Test</title>
    
    </head>
    
    <body>
    
    <a href="#">Remove this link</a>
    
    <script>
    
    alink = document.getElementsByTagName("a");
    for (i=0; i <= alink.length; i++){
    alink[i].style.display = "none";
    }
    
    </script>
    
    </body>
    
    </html>

    When loading the script in IE it says:

    alink[...].style is null or not an object


    Does anyone know how to fix this?


    Thanks!

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Instead of using javascript to search for every link and then hide each one, why not just use

    Code:
    <style type="text/css">
    a:link {
    display:none;
    }
    </style>
    However, the solution to your problem is:
    Code:
    for (i=0; i<=alink.length; i++){
    should be
    Code:
    for (i=0; i<alink.length; i++){
    This actually threw an error in FF as well, but FF just plowed on ahead and rendered it anyway. IE has a habit of not executing that part at all if there is an error.
    Last edited by binaryWeapon; 06-20-2008 at 04:53 AM. Reason: Went back to actually answer the question ^^

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You missed on adding a type attribute.
    YOu need to declare alink and i.

    I'm unaware where you want this be triggered, I suppose it's onload:
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    var alink = document.getElementsByTagName('a');
    for (var i=0; i <alink.length; i++)
    	{
    	alink[i].style.display = "none";
    	}
    }
    </script>
    Highlighted are added.

    Hope it helps.
    Edit:
    binaryWeapon beat me.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Thanks! It works perfectly now!


  •  

    Posting Permissions

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