Jump to content
Sign in to follow this  

Help with Tab

Recommended Posts

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>

<!-- 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 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 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 id="Seed" class="tabcontent">
  <p>Tokyo is the capital of Japan.</p>

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

Any help would be appreciated.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

It has to be in a <script> tag below the HTML that contains the active button, just as in the W3Schools example.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...