Skip Navigation

TypeError: openTab is not a function. but its still working..?

there's some javascript to make the tabs work, and a piece of it is to make one tab open by default. this doesn't work, however the error is with a part of the code that IS working.

https://perchance.org/life-series-generator

2

You're viewing a single thread.

2 comments
  • Since you are using a document.getElementById(...).click() on the button with the openTab HOWEVER, the openTab function is not yet defined since it is under the click() which is why it throws that error. So you just need to use the click() below the declaration of the function. I would also not use this as an Id of an element since this has a different use in JavaScript.

    <script>
      function openTab(evt, tabName){
        var i, tabcontent, tablinks;
        
        tabcontent = document.getElementsByClassName("content");
        for (i = 0; i < tabcontent.length; i++) {
          tabcontent[i].style.display = "none";
        }
        
        tablinks = document.getElementsByClassName("topbtn");
        for (i = 0; i < tablinks.length; i++) {
          tablinks[i].className = tablinks[i].className.replace(" active","");
        }
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.className += " active";
        
      }
      
      document.getElementById("TabOneBtn").click(); // click after declaring the function, changed the id of the button from 'this' to 'TabOneBtn'
    </script>
    

    Another possible way to fix your default is to probably just add the active class on the default one, and no need to click the button to set the default.