Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'style' of null - JS error

I'm getting a javascript error from my console that reads:

Uncaught TypeError: Cannot read property 'style' of null

Does anyone know whats wrong?

Code (JS):

<script>
    function select()
    {
        document.getElementById('#iframetest').style.display='block';
    }

</script>

Code (HTML):

<iframe src="localhost/createaclass" id="iframetest"></iframe>

<div class="b" onclick="select()">
like image 248
user3818791 Avatar asked Dec 11 '22 04:12

user3818791


2 Answers

Don't place the hash in id (#):

document.getElementById('iframetest')

As per your comment, you can do like this:

function select()
{
   document.getElementById('iframetest').style.display = 'block' ? 'none' : 'block';
}
like image 134
Bhojendra Rauniyar Avatar answered Apr 12 '23 22:04

Bhojendra Rauniyar


Move your JS below the HTML.

<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b" onclick="select()">

<script>
  function select()
  {
    document.getElementById('#iframetest').style.display='block';
  }
</script>
like image 45
Terrillo Walls Avatar answered Apr 12 '23 23:04

Terrillo Walls