0
    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        #b {
            display: none;
        }
    </style>
    <script type="text/javascript">
        document.getElementById("b").style.display = block;
    </script>
</head>
<body>
    <h1 id="b">Hello</h1>
</body>
</html>

Does anyone know what I'm doing wrong here? Shouldn't the CSS be changed by the javascript and display Hello. Thanks for any help!

3
  • Just remember accessing thru .style.<style> works only for inline styles. You should also wait until document is ready. Place script inside body Commented Sep 11, 2016 at 20:16
  • Move script tag after h1 ...and all should be OK...you trying to change value for something that doesn't exists at the time it executes. Commented Sep 11, 2016 at 20:19
  • 2
    Change block to "block", you aren't referencing a variable. And place your script after the body closing tag, that way it will be executed after parsing the HTML (the element "is there" to be selected by your code). Commented Sep 11, 2016 at 20:22

3 Answers 3

1
  <head>
<title></title>
<style type="text/css">
    #b {
        display: none;
    }
</style>

</head>
 <body>
  <h1 id="b">Hello</h1>
 <script type="text/javascript">
     document.getElementById('b').style.display = "block";
   </script>
  </body>
</html>

Try this its better to create custom.css file and link in head section. and also create custom.js file and link with your html befor /body tag end

Sign up to request clarification or add additional context in comments.

Comments

0

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        #b {
            display: none;
        }
    </style>
    
</head>
<body>
    <h1 id="b">Hello</h1>
</body>
</html>
<script type="text/javascript">
        document.getElementById("b").style.display = 'block';
    </script>

Put the block in quotes and move the <script> tag to the end.

Comments

0

You trying to change value of something that doesn't exists in the moment you execute JS...Move all JS before closing html tag like so

<script>Your code here<script>
</html>

here is the demo

https://jsbin.com/qacayuruyu/edit?html,js,output

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.