The toggling feature seems to be malfunctioning as the div section fails to display

I'm facing an issue with my Django project while working on a template. I want to toggle the visibility of a div element between hiding and showing, but the function I used isn't working for some reason. I borrowed the function from a different template where it worked fine, but now in my Django project, it's not functioning properly. I've been stuck trying to find a solution to this problem. Here is the template code:

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

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">


    <title>Admin Home Page</title>

    {% load staticfiles %}

    <!-- Bootstrap core CSS -->
    <link href="{% static 'Boards/Homepage/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{% static 'Boards/Homepage/css/modern-business.css' %}" rel="stylesheet">
    <style>
      #ManageUsers {
        display: none;
      }
    </style>

    <script type="text/javascript">
      function ToggleHide(id) {
        var divelement = document.getElementById(id);

        if (divelement.style.display === 'none')
          divelement.style.display = 'block';
        else
          divelement.style.display = 'none';
      }
    </script>
  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar fixed-top navbar-expand-lg fixed-top" style="background-color: #0b0849;">
      <div class="container">
        <a class="navbar-brand" href="#" style="margin-left: -120px; font-family: 'Sofia';font-size: 22px;" ><i class="fa fa-flash" style="font-size:24px"></i> Drello Admin</a>
        <div class="search-container">
          <form action="/action_page.php">
            <input type="text" placeholder="Search.." name="search">
            <button type="submit"><i class="fa fa-search"></i></button>
          </form>
        </div>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li>
              <a class="navbar-brand" href="index.html"><i class="fa fa-home" style="font-size:24px;"></i> Home</a>
            </li>
            <li class="nav-item dropdown" style="margin-right: -120px;">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> My Profile</a>

              <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-circle"></i> Profile</a>
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-plus"></i> Add User</a>
                <a class="dropdown-item" href="sidebar.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-bell"></i> Sidebar Page</a>
                <a class="dropdown-item" href="#" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-power-off"></i> Logout</a>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <br>
    <br>

    <!-- Page Content -->
    <div class="container">
      <!-- Page Heading -->
      <h2 class="my-4" style="margin-left: -50px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849;"><i class="fa fa-address-book-o"></i><a href = "" onclick="ToggleHide('ManageUsers');">  Manage Users </a> </h2>
      <div class="row" style="margin-left: -45px;">
        <div class="col-lg-3 col-md-4 col-sm-6 portfolio-item">
        </div>
      </div>
      <div id="ManageUsers">
        <table style="width:50%">
          <tr>
            <th>User Name</th>
          </tr>
          <tr>
            <td>Zeinab Awada</td>
            <td><a href = "#" > <button>Edit User</button></a><td>
          </tr>
          <tr>
            <td>Mahdi Jaber </td>
            <td><a href = "#" > <button>Edit User</button></a><td>
          </tr>
        </table>
      </div>
      <div class="container">
        <!-- Page Heading -->
        <h2 class="my-4" style="margin-left: -40px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849; padding-top: 20px;"><i class="fa fa-bars"></i> <a href = "" onclick="ToggleHide('ManageUsers');"> Manage Boards</a></h2>
      </div>
    </div>
    <!-- /.container -->

    <!-- Bootstrap core JavaScript -->

  </body>

</html>

Answer №1

By changing the `a` to `span`, the issue can be resolved. Alternatively, using `preventDefault()` is another option to prevent the anchor's default behavior.

Below is a modified stack snippet with `span` instead of `a`:

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

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">


    <title>Admin Home Page</title>
    {% load staticfiles %}
    <!-- Bootstrap core CSS -->
    <link href="{% static 'Boards/Homepage/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="{% static 'Boards/Homepage/css/modern-business.css' %}" rel="stylesheet">
     <style>
            #ManageUsers{
                           display :none;
                        }
    </style>
      <script type="text/javascript">
            function ToggleHide(id) {

                var divelement = document.getElementById(id);

                    if(divelement.style.display === 'none')
                        divelement.style.display = 'block';
                    else
                        divelement.style.display = 'none';
            }
    </script>
  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar fixed-top navbar-expand-lg fixed-top" style="background-color: #0b0849;">
      <div class="container">
        <a class="navbar-brand" href="#" style="margin-left: -120px; font-family: 'Sofia';font-size: 22px;" ><i class="fa fa-flash" style="font-size:24px"></i> Drello Admin</a>
        <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>
  </div>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li>
              <a class="navbar-brand" href="index.html"><i class="fa fa-home" style="font-size:24px;"></i> Home</a>
            </li>
            <li class="nav-item dropdown" style="margin-right: -120px;">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> My Profile</a>

              <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-circle"></i> Profile</a>
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-plus"></i> Add User</a>
                <a class="dropdown-item" href="sidebar.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-bell"></i> Sidebar Page</a>
                <a class="dropdown-item" href="#" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-power-off"></i> Logout</a>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <br>
    <br>
    <!-- Page Content -->
    <div class="container">
      <!-- Page Heading -->
        <h2 class="my-4" style="margin-left: -50px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849;"><i class="fa fa-address-book-o"></i><span onclick="ToggleHide('ManageUsers');">  Manage Users </span> </h2>
      <div class="row" style="margin-left: -45px;">
        <div class="col-lg-3 col-md-4 col-sm-6 portfolio-item">
      </div>
      </div>
        <div id="ManageUsers">
            <table style="width:50%">
  <tr>
    <th>User Name</th>
  </tr>
  <tr>
    <td>Zeinab Awada</td>
    <td><a href = "#" > <button>Edit User</button></a><td>


  </tr>
  <tr>
    <td>Mahdi Jaber </td>
    <td><a href = "#" > <button>Edit User</button></a><td>


  </tr>
</table>
        </div>
        <div class="container">
      <!-- Page Heading -->
      <h2 class="my-4" style="margin-left: -40px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849; padding-top: 20px;"><i class="fa fa-bars"></i> <span onclick="ToggleHide('ManageUsers');"> Manage Boards</span></h2>


      </div>
    </div>
    <!-- /.container -->

    <!-- Bootstrap core JavaScript -->


  </body>

</html>


Additionally, below is an updated code snippet with changes made to 'onclick' and the inclusion of 'preventDefault()':

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

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">


    <title>Admin Home Page</title>
    {% load staticfiles %}
    <!-- Bootstrap core CSS -->
    <link href="{% static 'Boards/Homepage/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="{% static 'Boards/Homepage/css/modern-business.css' %}" rel="stylesheet">
     <style>
            #ManageUsers{
                           display :none;
                        }
    </style>
      <script type="text/javascript">
            function ToggleHide(e) {
            
                e.preventDefault();

                var divelement = document.getElementById(this);

                    if(divelement.style.display === 'none')
                        divelement.style.display = 'block';
                    else
                        divelement.style.display = 'none';
            }
    </script>
  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar fixed-top navbar-expand-lg fixed-top" style="background-color: #0b0849;">
      <div class="container">
        <a class="navbar-brand" href="#" style="margin-left: -120px; font-family: 'Sofia';font-size: 22px;" ><i class="fa fa-flash" style="font-size:24px"></i> Drello Admin</a>
        <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>
  </div>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li>
              <a class="navbar-brand" href="index.html"><i class="fa fa-home" style="font-size:24px;"></i> Home</a>
            </li>
            <li class="nav-item dropdown" style="margin-right: -120px;">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> My Profile</a>

              <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-circle"></i> Profile</a>
                <a class="dropdown-item" href="full-width.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-user-plus"></i> Add User</a>
                <a class="dropdown-item" href="sidebar.html" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-bell"></i> Sidebar Page</a>
                <a class="dropdown-item" href="#" style="color: #0b0849; margin-bottom: 10px;"><i class="fa fa-power-off"></i> Logout</a>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <br>
    <br>
    <!-- Page Content -->
    <div class="container">
      <!-- Page Heading -->
        <h2 class="my-4" style="margin-left: -50px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849;"><i class="fa fa-address-book-o"></i><a href = "" onclick="ToggleHide.apply('ManageUsers',arguments);">  Manage Users </a> </h2>
      <div class="row" style="margin-left: -45px;">
        <div class="col-lg-3 col-md-4 col-sm-6 portfolio-item">
      </div>
      </div>
        <div id="ManageUsers">
            <table style="width:50%">
  <tr>
    <th>User Name</th>
  </tr>
  <tr>
    <td>Zeinab Awada</td>
    <td><a href = "#" > <button>Edit User</button></a><td>


  </tr>
  <tr>
    <td>Mahdi Jaber </td>
    <td><a href = "#" > <button>Edit User</button></a><td>


  </tr>
</table>
        </div>
        <div class="container">
      <!-- Page Heading -->
      <h2 class="my-4" style="margin-left: -40px; font-size: 16px; font-weight: 700; display: inline-block; color: #0b0849; padding-top: 20px;"><i class="fa fa-bars"></i> <a href = "" onclick="ToggleHide.apply('ManageUsers',arguments);"> Manage Boards</a></h2>


      </div>
    </div>
    <!-- /.container -->

    <!-- Bootstrap core JavaScript -->


  </body>

</html>


It is advisable to avoid inline scripts and instead use event listeners like the method described below to attach the click event:

document.querySelector('a css-selector').addEventListener('click', function(e) {

    // e holds the event and this holds the element that was clicked on

}); 

Answer №2

When using CSS to adjust the initial display of your divs, it's important to note that JavaScript won't automatically detect this style modification with

divelement.style.display

Instead, there are two alternative approaches you can take.

1. Embed the style directly into the div like so:

<div style=display:none>

And update your anchor tag as follows:

<a href="#" onclick="ToggleHide('ManageUsers');">  Manage Users </a>

With these adjustments, your code should function correctly without delay.

  1. Retrieve the computed style in the following manner:

function ToggleHide(id) {
    var divelement = document.getElementById(id);
    var displayStyle = divelement.currentStyle ? divelement.currentStyle.display : getComputedStyle(divelement, null).display;<br>
    if (displayStyle === 'none')
      divelement.style.display = 'block';
    else
      divelement.style.display = 'none';
}

In any case, ensure that your anchor's href attribute is set to # to prevent unintended navigation when the link is clicked.

I trust this response addresses your query satisfactorily.

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

Resetting Cross-Site Request Forgery (CSRF

Struggling to integrate Django's csrf with Angular 6? Check out this insightful thread I came across. It seems that Django changes the token on login, which makes sense as I can register and login using post requests but encounter issues posting after ...

Is it possible to retrieve data from a database using jQuery and store it in separate variables?

I am able to print out one field from a table, but I want to display all fields in separate tables. How can I achieve this? Below is my save/load code: // Save/Load data. $('').ready(function() { if($.cookie('code')) { $.aj ...

Converting API data in Angular using rxjs

Hey there, I received this response from an API: { "records":[ { "id":1, "motivazione":"", "autorizzazione":false, } ] } Can anyone help me transform it to loo ...

The Angular directive alters the scope, however, the template continues to display the unchanged value

I am working with a directive that looks like this: .directive('myDirective', function() { return { restrict: 'AE', replace: true, templateUrl: '/myDirective.html?v=' + window.buildNumber, ...

Vue-Router functions only on specific routes

While my vue-router correctly routes the URL for all menu buttons, it's not displaying each Vue component properly. You can see a demonstration here. Here is a snippet of my HTML (using Vuefy) <div class="navbar-start"> <a ...

What are the steps to integrate MaterializeCss into Vue.js?

I prefer not to utilize Vue-Material or Vuetify. My choice is Materialize. Here's what I do: npm install materialize-css@next In my main.js file, where I define my new Vue App, I import Materialize like this: import 'materialize-css' Th ...

The functionality of the JQuery $.post method may be compatible with Firefox but may encounter issues when

I've encountered an issue with my website where certain functions that update the database using $.post work perfectly in Firefox, but fail to function properly in Internet Explorer. I'm struggling to identify the root cause of this problem. Here ...

Ways to ensure that v-model does not become "true" or "false" in an input checkbox using Vue

I am currently working on a filter popup that includes a list of checkboxes. By default, some items should be selected and others not selected. I have connected these checkboxes to an object array using v-model. My issue is that when I deselect and select ...

Assigning index values to child rows in AngularJS: a step by step guide

One of my requirements involves assigning index values to child rows. The structure includes group rows with child rows underneath. Currently, I am using ng-repeat along with $index for the children as shown below: HTML code: <table ng-repeat="nod ...

`Warning: The alert function is not working properly in the console error

I am currently working on integrating otp functionality into my Ionic 3 project. I am facing an issue where I am able to receive the otp, but it is not redirecting to the otp receive page due to a specific error. Below is the console error that I am encou ...

Leveraging the power of the three.js library on the client-side within a vue.js/n

I'm facing a challenge with incorporating the three.js library (installed via npm) to display 3D models on the client side within my nuxt.js application. Despite multiple attempts, I seem to be hitting a roadblock with the import not functioning prope ...

What are some methods for simulating user interaction on input and button elements?

Here is a code snippet available in this stackblitz demo. Essentially, there is a basic form with an input field and a button. Clicking the button will copy the current value of the input to a label: https://i.stack.imgur.com/pw3en.png after click: htt ...

Question: How can I use the jQuery datepicker to ensure that the selected date remains in the

I recently created a webpage using a combination of HTML, PHP, and jQuery. The PHP script is designed to load all data where the date matches the one selected in the text input field generated by the jQuery datepicker. However, I encountered a problem whe ...

Interfacing Electron Frontend with Python Backend through seamless communication

Upon completing the development of a Python CLI app, it became apparent that creating an Electron frontend for better user interaction was necessary. What is the best way for the Electron app to communicate with the Python app when a user action occurs on ...

How to decode JSON data into a JavaScript array and retrieve specific values using index positioning

Upon receiving a json response via ajax, the code looks like this: echo json_encode($data); The corresponding ajax code is shown below: $.ajax({ url:"PaymentSlip/check", data:{val:val}, type: 'POST', succe ...

Navigating through nested arrays in Laravel Blade templates

I have an array that is displaying the code below after being dumped. array:1[ "123"=>array:3[ "test1" => 12345 "test2" => "test" "test3" => 123 ] ] When trying to display each element in an HTML table, the val ...

How to implement a Typescript interface without necessarily implementing the parent interfaces

Within my current project, I have defined the following interfaces: interface foo { fooProperty: number; fooFunction(): void; } interface bar extends foo { barProperty: string; barFunction(): void; } Now, I am interested in creating a class like ...

Overruled fashion

Here is the HTML code from my custom page: <div class="notes quotes-notification member-savings f-left"> <h2>My Notifications</h2> <ul> <li><b>I need to make some changes in the HTML, different from other pages you have ...

"Enhance your ASP.NET site with a captivating background

I am currently working with C# in VS2005. My challenge is to add a background image to my login page, which is in the form of an .aspx file. Here is a screenshot of my current setup: Here is the code snippet for my background: <div align="center" ...

merging the central pair of columns in the bottom row

I am having trouble combining the middle two columns in the third row of my table code. The columns remain separate and do not merge as intended. Can anyone help me identify what is causing this issue in my code? table { border: 1px solid #999; } td ...