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

Make a div with absolute positioning overflow outside of a div with relative positioning that is scrollable

I am facing an issue with two columns positioned side by side. The right column contains a tooltip that overflows to the left on hover. Both columns are scrollable and relatively positioned to allow the tooltip to follow the scroll. However, the tooltip is ...

Utilizing jQuery to iterate over dynamically generated elements sharing a common class

Whenever I click a button, numerous div elements are dynamically created. <table> <tbody id="ProductDetail"></tbody> </table> These dynamically created divs have an associated Amount value that is added upon creation. funtion ...

Ways to determine if a class is a part of the bootstrap framework

Is there a way to verify if a class like mt-3 or table-left is part of the bootstrap framework? Are there any online resources, tools, or plugins in VSCode that can assist me in determining if a class is a predefined bootstrap keyword? ...

Can you provide instructions on how to insert a hyperlink onto a background image?

Is it possible to add a hyperlink to this background image without causing it to disappear? Would creating a new class in the stylesheet be the way to go? (I encountered an issue where the image disappeared when trying to call the new class). body{ back ...

Exploring Django's querying capabilities with sets

Seeking guidance as a Django newcomer facing challenges with querying through multiple sets. In my project, there are three essential models; class Project(models.Model): name = models.CharField(max_length = 100) class AppointmentGroup(models.Model) ...

What could be causing my Bootstrap carousel to only slide once?

I'm currently working on integrating a Bootstrap Carousel into my website, but I've encountered an issue where it moves only once and then becomes unresponsive. After checking the file order, I couldn't find any issues there. So, I'm qu ...

Unique ways to serialize an HTML element efficiently: JavaScript tricks

Is there a way to store a reference of an HTML tag for future use? For instance, if I click on a div and save a pointer to that div in JavaScript, is it possible to serialize this pointer and then de-serialize it to use in another part of the web applicat ...

Implementing a function to save a file to a nested directory in node.js

Currently, I'm utilizing node and express with the following directory structure: public img css js server.js Within server.js, I have the code snippet below for writing to a png file: fs.writeFile('testfile.png', imageData, func ...

Enhancing State Management with CombineReducers in TypeScript

export const rootReducer = combineReducers({ login: loginReducer, }); Everything is working fine with the current setup, but I encountered an issue when attempting to combine another reducer: export const rootReducer = combineReducers({ login: lo ...

Fetch search results dynamically in Wordpress through AJAX

I'm struggling to implement AJAX on my WordPress site to display search results without refreshing the page. Despite trying various solutions found through research, none seem to be working effectively for me. Currently, here is the progress I have ma ...

Detecting when the "enter" key is pressed using Jquery instead of relying on a mouse click

One issue I am facing is that jQuery is detecting the "enter" key press instead of mouse clicking on the submit button when trying to submit a form. The submit button works fine on the first attempt, but after that it only responds to the "enter" key. He ...

What is the best way to extract data from a foreign key in Django?

I am attempting to retrieve the vendor information based on the category ID. This means that when a category is selected, all vendors related to that category should be displayed. However, when I call the URL, an error is shown: 'Cannot use QuerySet ...

Angular Unit testing error: Unable to find a matching route for URL segment 'home/advisor'

Currently, I am working on unit testing within my Angular 4.0.0 application. In one of the methods in my component, I am manually routing using the following code: method(){ .... this.navigateTo('/home/advisor'); .... } The navigateTo funct ...

What is causing this to function properly on Firefox but not on Chrome or IE?

After clicking the process_2banner button on my html page, a piece of code is executed. Surprisingly, this code performs as expected in Firefox, but not in Chrome or Internet Explorer. In those browsers, although the ajax code is triggered, the div spinner ...

Issue with npm configuration permissions

I am encountering permission issues when using the npm config command. It appears that there is an attempt to alter the owner of my ~/.npmrc file without authorization. Upon executing npm config set color false, I encounter the following error: npm ERR! E ...

JavaScript: Retrieving the coordinates of the visible area of an element

Is there a way to calculate the visible area of an element on the screen, taking into account any hidden parts due to CSS properties like overflow: scroll and position: absolute? The goal is to create a function called getVisiblePart(el) that will return ...

What is the best way to show only one div at a time when selecting from navbar buttons?

To only display the appropriate div when clicking a button on the left navbar and hide all others, you can use this code: For example: If "Profile" is clicked in the left navbar, the My Profile Form div will be displayed (and all others will remain hidde ...

Click on the react item to view more information

I'm brand new to React and I'm exploring the swapi API for the first time. My goal is to retrieve a list of films (movie titles) and then, when a title is clicked on, display the opening crawl from the JSON object. So far, I've been able to ...

How do I resolve the jQuery error "unable to find function url.indexOf" when working with Vide?

I had previously asked a question regarding the use of the Vide plugin for playing background videos on my website, which can be found here. However, I am now encountering an error that is puzzling me: https://i.stack.imgur.com/UDFuU.png I am unsure abo ...

Array not transmitted via jQuery ajax

I am attempting to use the jQuery ajax function to send an array, but for some reason it is not functioning as expected. Below is the code I have been working with: if (section_name == "first_details_div") { var fields_arr = ["f_name", "l_name", "i ...