0

I am creating a website, using html, bootstrap css and jquery. This is the html code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="node_modules/bootstrap-social/bootstrap-social.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Exo&display=swap">
    <link rel="stylesheet" href="styles.css">

    <title>TimeGuard</title>
</head>

<body>
    <div class="main">
        <div>
            <nav class="navbar">
                <ul id="firstul" class="nav nav-tabs nav-justified">
                    <li class="nav-item active" style="width:200px"><a class="nav-link"
                            href="#dashboard">DASHBOARD</a></li>
                    <li class="nav-item inactive" style="width:200px;"><a class="nav-link"
                            href="#reports">REPORTS</a></li>
                    <li class="nav-item inactive" style="width:500px;"><a class="nav-link"
                            href="#addict">ADDICT SETTINGS</a></li>
                </ul>
            </nav>
        </div>

        <div class="tab-content" style="padding:20px;">
            <div class="tab-pane fade show active" role="tab-panel" id="dashboard">
                <div>
                    <h1>OVERVIEW</h1>
                    <div class="container-fluid box">
                        <div class="row justify-content-around">
                            <div class="overview col-3">
                                <div style="font-size:79px;">
                                    00:00:00
                                </div>
                            </div>
                            <div class="overview col-3">
                                <div style="font-size:79px;">
                                    00
                                </div>
                            </div>
                        </div>
                        <div class="row justify-content-center" style="margin-top:50px;">
                            <div class="overview col-3">
                                <div style="font-size:79px;">
                                    00:00:00
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div>
                    <h1>TARGET WEBSITES</h1>
                    <div class="box container-fluid">
                        <div class="row justify-content-center">
                            <div class="tarweb col-7">
                                TARGET WEBSITES
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <div class="tab-pane fade" role="tab-panel" id="reports">
        <div class="box">
            
        </div>
    </div>

    <script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
    <script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="scripts.js"></script>
</body>

</html>

Following is the jquery code, with a purpose to switch the between the navigation tabs (DASHBOARD, REPORTS and ADDICT SETTINGS). But the action does not take place on all the tabs.

$(document).ready(function(){
    $("#firstul li.inactive").on("click",function(){
        $("#firstul li.active").removeClass("active").addClass("inactive");
        $(this).removeClass("inactive").addClass("active");
    });
});

I will be glad if someone could help me out.

Also this is the CSS code if required:

body{
    font-family: 'Exo', sans-serif;
}

.main{
    background-color:rgb(4, 209, 4);
    margin:30px;    
    border-radius:10px;
    padding-top: 0px;
    padding-bottom:20px;
}

.navbar{
    background-color:white;
    padding-left:0px;
    padding-right:0px;
    margin-left:auto;
    margin-right:auto;
    padding:0px;
}

.nav-link{
    color:black;
}

li{
    font-size:25px;
    text-align:center;
}

li.active{
    background-color: rgb(4, 209, 4);
    color:black;
    border:5px double;
    border-bottom:none;
}

li.inactive{
    background-color: green;
    
}

.box{
    border:1px solid rgb(92, 86, 86);
    border-radius:10px;
    padding:20px;
}

.overview{
    background-color: white;
    padding:5px;
    justify-content:center;
    border-radius:50px;
    text-align:center;
}

.tarweb{
    background-color: white;
    padding:5px;
    justify-content:center;
    border-radius:50px;
}

2 Answers2

0

Try to have look at this entry I don't think that removing and adding classes is the way to go. Or is this what you want?

Rahm6
  • 38
  • 7
0

Try:

$(document).ready(function(){
    $("#firstul li").on("click",function(){
        $("#firstul li").removeClass("active").addClass("inactive");
        $(this).removeClass("inactive").addClass("active");
    });
});

The problem in your solution is that you do not add the click handler to the active tab item. In the solution above we:

  1. Add the handler to all tab items
  2. On click, inactivate all tab items
  3. Activate only the clicked item
buddha
  • 141
  • 10