Fellucca Posted March 20, 2020 Share Posted March 20, 2020 I installed the tab from here https://www.w3schools.com/howto/howto_js_tabs.asp and it works great. However, when I added and adjusted the code for "show a tab by default" it is not working. Here is my HTML: <!-- Tab links --> <div class="tab"> <button class="tablinks" onclick="openCity(event, 'Cultivate')" id="defaultOpen">Cultivate</button> <button class="tablinks" onclick="openCity(event, 'Weed')">Weed</button> <button class="tablinks" onclick="openCity(event, 'Mulch')">Mulch</button> <button class="tablinks" onclick="openCity(event, 'Seed')">Seed</button> </div> <!-- Tab content --> <div id="Cultivate" class="tabcontent"> <p>When easily rolled back and forth over the ground, the Spintiller digs then lifts the soil just an inch or two creating the optimal condition for water, air, and nutrient penetration.</p> </div> <div id="Weed" class="tabcontent"> <p>With a simple flip, the Spintiller’s narrow path is great for loosening and pulling weeds,lessening the need for harmful weedicides. Use this in and around your precious plants and vegetables reducing root, leaf and stalk damage.</p> </div> <div id="Mulch" class="tabcontent"> <p>The Spintiller is also perfect for dressing your mulch beds. It does quick and easy work of turning over mulch without tossing it all over the place – invigorating your beds and keeping your mulch healthy looking.</p> </div> <div id="Seed" class="tabcontent"> <p>Tokyo is the capital of Japan.</p> </div> And here is my JS: function openCity(evt, cityName) { // Declare all variables var i, tabcontent, tablinks; // Get all elements with class="tabcontent" and hide them tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent.style.display = "none"; } // Get all elements with class="tablinks" and remove the class "active" tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks.className = tablinks.className.replace(" active", ""); } // Show the current tab, and add an "active" class to the button that opened the tab document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click(); Any help would be appreciated. Link to comment Share on other sites More sharing options...
Ingolme Posted March 20, 2020 Share Posted March 20, 2020 The code works OK for me, but you have to put the "defaultOpen" line of Javascript further down in your code than the element you are trying open. It can't be in the external Javascript file if that file is being loaded in the head of the document. Link to comment Share on other sites More sharing options...
Fellucca Posted March 20, 2020 Author Share Posted March 20, 2020 Do you mean the "defaultOpen" line has to be in the HTML? Link to comment Share on other sites More sharing options...
Ingolme Posted March 20, 2020 Share Posted March 20, 2020 It has to be in a <script> tag below the HTML that contains the active button, just as in the W3Schools example. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now