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
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    newbie. Don't understand why CSS styles don't set DOM properties

    Code:
    <style type="text/css">
    a {display: none;}
    </style>
    then in a function
    Code:
     var allLinks = document.getElementsByTagName("A")
         then in a loop of all links:
    alert("88>" + allLinks[i].style.display + "<<")
    allLinks[i].style.display = "block"
    alert("99>" + allLinks[i].style.display + "<<")
    For the first Alert the display property has no value, but I'd expect "none"; what's the reason? (Note setting "block" does make all the links visible). I'm using IE 6.0.2 FWIW.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You aren't seeing the results of what a "class" applies to individual elements because the CSS isn't applied to the individual elelements.

    Remember the "C" in CSS means "Cascading" which implies some levels.

    Remember also that you can apply styles at the parent level, class level and then the individual level. In order to do that you've got to be able to keep separate locations for those values so in this case, since you've set "display" at the class level, looking for it's value at the individual item level isn't going to give you a result.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    [object HTMLElement].style reflects properties set in the style attribute. Not actually applied styles.

    Use some DOM2 CSS:

    document.defaultView.getComputedStyle([object HTMLElement], "").getPropertyValue("display")

    And IE-proprietary fudgery:
    [object HTMLElement].currentStyle.display


  •  

    Posting Permissions

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