Projects

Create Messenger Pop Up ChatBox Type Writer ChatGpt Basic Copy Text Using Tolltip Digital Clock CountDown Timer Create StopWatch FB login from Tab Button Accordion Auto Image Gallery DropDown Menu Modal Box Modal Image Box Scroll Indicator To Do List Autocomplete Remember Search History Toggle Switch Calender Layout Dynamic Calender Alarm Set Password Validation Password Generator Age Calculator Multi Step Form Weather API

How To Use Tab Button💻

Note: If you are a beginner, this is something you need to know. Scroll the below section or Click here
  • Button in HTML
  • Event in JS
  • onclick & click in JS

Click the below buttons and See how to use tab button

Dhaka

Dhaka is the capital of BD

London

London is the Capital of England.

Tokyo

Tokyo is the Capital of Japan.

HTML


<div class="showcontent">
<h3>Click the below buttons and See how to use tab button</h3>
<div class="btnclass">
<button class="btn" onclick="myFun('Dhaka',event)" id="default">Dhaka</button>
<button class="btn" onclick="myFun('London',event)">London</button>
<button class="btn" onclick="myFun('Tokyo',event)">Tokyo</button>
</div>
<div class="tabcontent" id="Dhaka">
<h2>Dhaka</h2>
<p>Dhaka is the capital of BD</p>
</div>
<div class="tabcontent" id="London">
<h2>London</h2>
<p>London is the Capital of England.</p>
</div>
<div class="tabcontent" id="Tokyo">
<h2>Tokyo</h2>
<p>Tokyo is the Capital of Japan.</p>
</div>
</div>

JavaScript


function myFun(cityname,event){
    let cont=document.querySelectorAll(".tabcontent");

    for(let i=0;i< cont.length;i++){
        cont[i].style.display="none";
        
    }

    let btnlist=document.querySelectorAll(".btn");
    for(let i=0;i< btnlist.length;i++){
        btnlist[i].className=btnlist[i].className.replace(" show","");
        
    }

    document.getElementById(cityname).style.display="block";
    event.target.className+=" show";

}

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

CSS


.btnclass{
    overflow: hidden;
    border-radius: 5px;

}
.showcontent button{
    float:left;
    border: none;
    padding: 12px 16px;
    font-size: 17px;
    cursor: pointer;
    background-color: #ddd;
    
}
.showcontent button:hover{
    background-color: #bbb;
    color: black;
}
.tabcontent{
    padding: 12px 16px;
    background-color: #ddd;
    border-bottom: 2px solid #ccc;
}
.showcontent .show{
    background-color: dodgerblue;
    color: white;
}

HTML DOM

  • querySelector: retuns the first element within the document that matches the specified CSS selctor. Example: const sampleElement = document.querySelector('.sample').If there are mutltiple element with the class name 'sample' `querySelector` will return the first matching elements.
  • You can also use getElementById, getElementByClassName, querySelectorAll etc

String charAt()

  • The charAt(): method in JavaScript returns the character at a specified index in a string. The index starts from 0. Example: const str = "Hello, World!";
    const char = str.charAt(7)

setTimeOut method()

  • setTimeOut(function-name, millsec): that will execute the provided function after certain millisecond.
    clearTimeout(timeoutId) used to cancel time out if needded.

requestAnimationFrame()

  • requestAnimationFrame(): works by calling the provided callback function before the next repaint, allowing smooth animations.
    Example requestAnimationFrame(function_name);