The first tab does not automatically open automatically on pageload

I am trying to create a page where the content changes when you click on the tab. I got the base code from w3school, but I cannot open the first tab when the page loads. Please help me find what I am doing wrong. I start with javascript.

Here's the code I'm using (this is the code from w3school, which should have the first tab open when the page loads.). I have a script in an external file and there is a script tag in the header section.

<div class="tab"> <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button> <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button> <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button> </div> <div id="London" class="tabcontent"> <h3>London</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Paris</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>Tokyo</h3> <p>Tokyo is the capital of Japan.</p> </div> <script> function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click(); 

Here is the site http://pt6-dev.info/donate-now/

Any help is appreciated! Peggy

+5
source share
3 answers

Add the following line of code at the end of your JavaScript

 window.onload=function(){ openCity(event, 'London'); }; 
+4
source

It does not work because you placed your JS in the head, so this statement:

 document.getElementById("defaultOpen").click(); 

executed before creating the DOM.

The easiest solution is to move your JS file to the end of the <body> .

+2
source

Replace your Tab.js with the following code :)

You need to call document.getElementById("defaultOpen").click(); when the presentation will be prepared.

 function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } window.onload = function(e){ document.getElementById("defaultOpen").click(); } $(document).ready(function() { //If you are using jquery then use this other wise you can use normal window.onload document.getElementById("defaultOpen").click(); }); 
+1
source

Source: https://habr.com/ru/post/1276254/


All Articles