Skip to content Skip to sidebar Skip to footer

Tab Content Loaded By Default

This might be a trivial question but I would like to know how to have a particular tab opened as the page loads itself. In the following, only the tab menus are there and the conte

Solution 1:

Your can do this in css define your tabcontent first display block as like this

.tabcontent:nth-of-type(1){display:block;}

functionopenCity(evt, cityName) {
    // Declare all variablesvar i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].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[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the link that opened the tabdocument.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}
/* Style the list */ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */ul.tabli {float: left;}

/* Style the links inside the list items */ul.tablia {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */ul.tablia:hover {background-color: #ddd;}

/* Create an active/current tablink class */ul.tablia:focus, .active {background-color: #ccc;}

/* Style the tab content */.tabcontent {
    display: none;
    padding: 6px12px;
    border: 1px solid #ccc;
    border-top: none;
}
.tabcontent:nth-of-type(1){display:block;}
<ulclass="tab"><li><ahref="#"class="tablinks"onclick="openCity(event, 'London')">London</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Paris')">Paris</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Tokyo')">Tokyo</a></li></ul><divid="London"class="tabcontent"><h3>London</h3><p>London is the capital city of England.</p></div><divid="Paris"class="tabcontent"><h3>Paris</h3><p>Paris is the capital of France.</p></div><divid="Tokyo"class="tabcontent"><h3>Tokyo</h3><p>Tokyo is the capital of Japan.</p></div>

============ 2nd option is you can deinfe in html by default show to one

functionopenCity(evt, cityName) {
    // Declare all variablesvar i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].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[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the link that opened the tabdocument.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}
/* Style the list */ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */ul.tabli {float: left;}

/* Style the links inside the list items */ul.tablia {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */ul.tablia:hover {background-color: #ddd;}

/* Create an active/current tablink class */ul.tablia:focus, .active {background-color: #ccc;}

/* Style the tab content */.tabcontent {
    display: none;
    padding: 6px12px;
    border: 1px solid #ccc;
    border-top: none;
}
<ulclass="tab"><li><ahref="#"class="tablinks active"onclick="openCity(event, 'London')">London</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Paris')">Paris</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Tokyo')">Tokyo</a></li></ul><divid="London"class="tabcontent"style=display:block;><h3>London</h3><p>London is the capital city of England.</p></div><divid="Paris"class="tabcontent"><h3>Paris</h3><p>Paris is the capital of France.</p></div><divid="Tokyo"class="tabcontent"><h3>Tokyo</h3><p>Tokyo is the capital of Japan.</p></div>

Solution 2:

One solution can be to simulate a click.

add this :

document.getElementsByClassName("tablinks")[0].click();

functionopenCity(evt, cityName) {
    // Declare all variablesvar i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].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[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the link that opened the tabdocument.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}

document.getElementsByClassName("tablinks")[0].click();
/* Style the list */ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */ul.tabli {float: left;}

/* Style the links inside the list items */ul.tablia {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */ul.tablia:hover {background-color: #ddd;}

/* Create an active/current tablink class */ul.tablia:focus, .active {background-color: #ccc;}

/* Style the tab content */.tabcontent {
    display: none;
    padding: 6px12px;
    border: 1px solid #ccc;
    border-top: none;
}
<ulclass="tab"><li><ahref="#"class="tablinks"onclick="openCity(event, 'London')">London</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Paris')">Paris</a></li><li><ahref="#"class="tablinks"onclick="openCity(event, 'Tokyo')">Tokyo</a></li></ul><divid="London"class="tabcontent"><h3>London</h3><p>London is the capital city of England.</p></div><divid="Paris"class="tabcontent"><h3>Paris</h3><p>Paris is the capital of France.</p></div><divid="Tokyo"class="tabcontent"><h3>Tokyo</h3><p>Tokyo is the capital of Japan.</p></div>

Solution 3:

try

.tabcontent:first-child{
    display:block;
}

Post a Comment for "Tab Content Loaded By Default"