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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    js does not read style from css

    I would expect that alert would pop up 'none'.
    How to solve that ?

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    <style type="text/css">
    
    .myclass
    {
      display:none;
    }
    </style>
    
    <script>
    function demoDisplay()
    {
    alert(document.getElementById("p1").style.display);
    }
    
    
    </script>
    </head>
    <body>
    
    <p id="p1" class = 'myclass'>This is some text.</p>
    
    
    <input type="button" onclick="demoDisplay()" value="see if js can get me style from css">
    
    
    </body>
    </html>
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    There is getComputedStyle() together with getPropertyValue(). The .style property will only get inline styles btw.

    Code:
    var element = document.getElementById('p1');
    alert(window.getComputedStyle(element, null).getPropertyValue('display'));

  • Users who have thanked devnull69 for this post:

    BubikolRamios (11-22-2012)


  •  

    Posting Permissions

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