The navigation bar fails to collapse after the page loads within a specific div using JavaScript

I've been encountering issues with my navbar. Whenever I try to collapse it after clicking on one of the options, it gets stuck and can only be fixed by refreshing the page.

Here is the html code used for the page:

<!DOCTYPE html>
<html>
<head>
    <title>Main Page</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" href="../css/bootstrap.css">
    <link rel="stylesheet" href="../css/Style.css">

</head>
<body>
    <div class="">
        <header>
            <nav class="navbar navbar-inverse" id="nav">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-1">
                            <span class="sr-only">Menu</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a href="" class="navbar-brand">Ans Dashboard</a>
                    </div>
                    <div class="collapse navbar-collapse text-center" id="navbar-1">
                        <ul class="nav navbar-nav">
                            <li class="" data-target="customer_main"><a href=""><span class="glyphicon glyphicon-user"></span><?php echo " Customers";?></a></li>
                            <li class="" data-target="inventorypage"><a href=""><span class="glyphicon glyphicon-th-list"></span><?php echo " Inventory";?></a></li>
                            <li class="" data-target=""><a href="#"><span class="glyphicon glyphicon-signal"></span><?php echo " Stats";?></a></li>
                            <li class="" data-target=""><a href="#"><span class="glyphicon glyphicon-asterisk"></span><?php echo " Settings";?></a></li>
                        </ul>
                        <div class="navbar-right text-center">
                            <ul class="nav navbar-nav">
                                <form method="post" class="nav navbar-nav">
                                    <li><p class="navbar-text"><?php echo "Welcome"?><p></li>
                                    <li><button type="submit" name="logout" id="logout" class="btn btn-danger btn-sm navbar-btn"><span class="glyphicon glyphicon-off"></span><?php echo " Log out";?>
                                    <?php
                                        include("logout.php");
                                    ?>
                                    </button></li>
                                </form>
                            </ul>
                        </div>
                    </div>
                </div>
            </nav>
        </header>
    </div>
    <!-- end of navbar-->
    <div id="content">
         <!-- Js page loads here -->
    </div>

    <script src="../js/jquery-3.1.1.min.js"></script>
    <script src="../js/mainpage.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    </body>
</html>

This is the javascript responsible for loading pages when the navbar area is clicked (mainpage.js)

$(document).ready(function()
{
var trigger = $('#navbar-1 ul li'), container = $('#content');

trigger.on('click', function(){

    var $this = $(this), target = $this.data('target');

    container.load(target + '.php');

    trigger.removeClass("active");
    $this.addClass("active");

    return false;

    });
});

UPDATE: The issue has been resolved, as it was related to the structure of the loaded pages.

Answer №1

I have made some updates to your code, please review the modifications in the link provided below:

$(document).ready(function() {
  var tab = $('#menu-1 ul li'),
    display = $('#section');
  tab.on('click', function() {
    var $tabSelected = $(this),
      targetTab = $tabSelected.data('target');
    display.load(targetTab + '.html');
    tab.removeClass("active");
    $tabSelected.addClass("active");
    return false;
  });
});

Link to see changes

Thank you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create an Interactive Form Generator in Angular

I'm currently working on an Angular project that involves Dynamic Inputs. When I click on a button, new inputs are created and now I want to use FormBuilder to get the data. However, I am unsure of how to go about this. Although I am familiar with us ...

Error encountered: Unexpected '<' token when trying to deploy

Trying to deploy a React app with React Router on a Node/Express server to Heroku, but encountering the following error... 'Uncaught SyntaxError: Unexpected token <' Suspecting the issue may lie in the 'catch all' route in the Expr ...

A beginner's guide to implementing RoundSliderUI in Angular 6

I've successfully integrated a range input in my Angular application, along with jQuery. The roundSlider CDN was included after the jQuery CDN in index.html. Then, within one of the components, I added the following HTML code: <div id="range">& ...

The PrivateRoute component from React-Router-Dom prevents page rendering and redirects to previous routes

I'm experiencing some routing issues when authenticated. Whenever I attempt to access my ViewPortfolio page at localhost:3000/portfolio/portfolioId, it redirects me back to the homepage. I'm uncertain about the cause of this behavior. Even when I ...

Guide on implementing a horizontal slide effect for background images

I am working on a mobile web application and I would like to create a homepage with an animated background image that slides like a portrait. For example, if the mobile screen width is 360px and the background image size is 500px, I want the image to star ...

Vue.js's two-way data binding feature is currently functioning in only one direction

Here is an example of my code: <input type="text" v-model="selectedPost.title" v-bind:value="selectedPost.title"> and this is my Vue.js application: var app = new Vue({ el: '#app', data: { selectedPost: {} } }); ...

What is the best way to display cards next to each other on desktop and mobile devices while maximizing available space?

In the world of React components, there exists a card representation within a grid view that adapts based on available screen space; function EngineerCard(engineer: EngineerDetail) { return ( <div className='engineerCard'> <d ...

Verify the status of the checkbox to determine if the user has enabled or disabled

To check the condition based on whether a user is enabled or not, I have implemented a checkbox feature. By default, the checkbox is checked, signifying that the user is enabled. However, if the user wishes to update their status, they can simply click on ...

Having trouble getting Angular-UI-Select to work with a basic variable on $scope?

How can you reset an array with selected values so that the values can be reselected from the dropdown? I am working with a people array where the values are initially displayed in a select dropdown. When I choose certain names, they get transferred to th ...

Having trouble utilizing {{MEDIA_URL}} in Django Flatpages?

Here's a question that I can provide more details on if necessary to find a solution. I tried using a simple {{ MEDIA_URL }} call to display an image on an About Us Django flatpage, but it appears that the flatpage is not recognizing it as a Django v ...

Javascript - Editing specific markers with varying titles from the url

My website contains checkboxes in the html code: <input onclick="markAsChanged(this); toggleApproveDeny(this);" name="2c9923914b887d47014c9b30b1bb37a1_approveChk" type="checkbox"> <input onclick="markAsChanged(this); toggleApproveDeny(this);" na ...

Issue with OpenLayers Icon not appearing on screen

I just finished creating a SpringBoot app using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and packaging it as an executable JAR file. Within this app, I have integrated OpenLayers 4 library to display a map with an Icon. Howeve ...

Can you provide the date time format used in the JSTL fmt tag?

To display date and time in JSTL, the fmt tag can be utilized. Details can be found here In order to format the date for use with front end tools like the datatable, a specific date format needs to be specified. By using parameters such as type or dateSty ...

Proper techniques for testing an AngularJS Controller Function

Recently, we integrated jasmine tests into our AngularJS project and I have a query: We are looking to write tests for this controller function: $scope.deleteClick = function () { $scope.processing = true; peopleNotesSrv.deleteNote($scope ...

Incorporating the chosen value from a dropdown box as a variable in PHP

Seeking assistance with using the value selected in a drop-down box as a PHP variable in my functions.php file. I attempted to utilize jquery-ajax for this purpose. $(document).ready(function(){ $("#pa_color").change(function(){ var result= $(this).val( ...

Redux's 'connect' function fails to recognize changes in the state array

I've recently implemented redux with a reducer that handles an array of time slots for a specific date. Whenever the date is changed, the reducer successfully updates the state (confirmed through console logs in my mapStateToProps function). However, ...

What sets apart `const [a, b, c] = array;` from `const {a, b, c} = array;`?

let [x, y, z] = arr; let {x, y, z} = obj; Q: Can you explain the distinction between these two declarations? ...

Page elements subtly move when reloading in Chrome

I am experiencing an issue with a div that has left and top offsets randomly selected from an array of values upon page load. Most of the time, it works fine. However, occasionally, upon refreshing the page, the window scrolls down slightly, revealing the ...

Attempting to access the server route does not involve the function str.CharAt

I seem to have encountered an issue while trying to deploy an API to Heroku. Initially, everything was running smoothly on my local machine, but at some point, I must have made a mistake, resulting in a broken setup. When I access the page using just local ...

Utilizing a for loop to iterate through an array based on its length

Just delving into the world of JavaScript and recently grasped the concept of For loops. I have a variable that holds an array with a list of first names, and my goal is to add last names to each of them. Here's the code snippet I came up with: l ...