SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should only run code that alters the DOM after the page is completely loaded. The object tree remains undefined until then; hence the "null or not an object" message. What you need to do is put your code in a function and call that function via the window or document body's onload handler.

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.onload = function()
    {
    //find links and set display to none here
    }

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Why isn't a style set to achieve this instead?

    Code css:
    a {
        display: none;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Why isn't a style set to achieve this instead?

    Code css:
    a {
        display: none;
    }
    Yeah, this should be the most simple and effetive solution.

  6. #6
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

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