I'm currently facing an issue with toggling the visibility of a div using JavaScript

In my attempt to create a unique custom select menu using html, css, and javascript, I encountered an issue with toggling the display style of the div containing the options when clicking on the button (in this case, an arrow).

HTML:
<ul class="default_option_category_index">
              <li>
                <div class="option_allcategories">
                    <p>All Categories</p>
                    <button id="ArrowCategoryIndex" class="fa fa-chevron-up" onclick="showCategoriesIndex()"></button>
                </div>
              </li>
            </ul>
 <ul id="OptionsCategoryIndex" class="select_option_category_index">
//options
</ul>
CSS:
#ArrowCategoryIndex{
  transform: rotate(180deg);
}

.custom_select_box_1_index .select_option_category_index{
  background-color: var(--white);
  border-radius: 10px;
  box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.05);
  z-index: 1;
  display: none;
}
JavaScript:
function showCategoriesIndex() {
  var arrow = document.getElementById("ArrowCategoryIndex"); 
  var option_menu = document.getElementById("OptionsCategoryIndex");
  if (option_menu.style.display === "none"){
    option_menu.style.display = "block";
    arrow.style.transform = "rotate(0deg)";
  }
  else if (option_menu.style.display = "block"){
    arrow.style.transform = "rotate(180deg)";
    option_menu.style.display = "none";
  }

}

The challenge lies in the functionality of the "else if" part of the JavaScript code. Upon clicking the button for the first time, the menu displays properly. However, subsequent clicks do not toggle the menu as intended.

Answer №1

The if statement in your code is not making a proper comparison. Instead of comparing, you are using the assignment operator '='. It should be changed to '===' for a correct comparison.

if (option_menu.style.display === "none"){
    option_menu.style.display = "block";
    arrow.style.transform = "rotate(0deg)";
  }
  else if (option_menu.style.display === "block"){
    arrow.style.transform = "rotate(180deg)";
    option_menu.style.display = "none";
  }

Be sure to change it to use '===' instead of '=' for comparison.

Answer №2

Consider attempting the following:

option_menu.style.display = "block"
can be changed to
option_menu.style.display === "block"

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

Arranging the Bars in a Bar Chart Using Chart.JS

Lately, I've been playing around with ChartJS and encountering an issue with sorting the bars in descending order, from lowest to highest. Despite my efforts to troubleshoot, I haven't had any success in resolving it. ...

Where is the source of this ever-changing image?

Recently, I decided to dive into the world of jQuery and experiment with a plugin carousel. However, I must admit it is all a bit overwhelming for me at the moment. Currently, I have the plugin installed and I am struggling to get rid of the bottom scroll ...

Troubleshooting Java REST service integration in AngularJS for UPDATE and DELETE operations

After successfully implementing a REST service with Java and testing all HTTP methods using Postman, I decided to explore AngularJS. Upon integrating it to consume the REST service, I encountered issues specifically with the Delete and Put methods not func ...

Loading Data in CodeMirror using XMLHttpRequest (XHR)

Is there any progress in loading the content into CodeMirror using XHR? ...

No data found in Node.js after receiving AngularJS POST request

I've been working on sending a straightforward POST request to my server using AngularJS. The request successfully goes through and reaches the controller on the backend, but strangely, req.data is appearing as undefined. Front End Controller: funct ...

Leveraging the power of Promise creation

I am facing an issue where I am trying to utilize the getPromise function with various URLs in order to obtain different promises, but encountering undefined values in the success function of the second promise. var http=require('http'); var URL ...

Finding the position of the biggest floating point number in the array

What is the best way to find the index of the largest element in an array of floating point numbers? [0.000004619778924223204, 0.8323721355744392, 0.9573732678543363, 1.2476616422122455e-14, 2.846605856163335e-8] Once the index of the largest element is ...

In a jQuery conditional statement, selecting the second child of the li element for targeting

I'm encountering an issue where I can't target the second child of an li element and use it in a conditional statement. Are jQuery conditionals not compatible with li:nth-child(2)? if($(".steps ul li:first-child").attr('aria-selected') ...

React web app experiencing an issue with MUI Drawer opening flawlessly but failing to close

Recently, I encountered an issue with my React app. I have a Navbar component that displays a Sidebar component when the screen is small, using Material UI for the Drawer functionality. The problem arises when clicking the hamburger icon to open the drawe ...

Using Angular with a hapi.js server that supports JSONP data requests

In my project, there is an endpoint specifically set at /api/profile that accepts post parameters. var http = require('http'); var serverConfig = require('../server.config.js'); var request = require('request'); module.expo ...

What could be causing my React child component to not update when changes are made to an array passed down in props after fetching new data?

My Profile.js component is responsible for fetching activity data related to a specific user from the URL parameter and updating the profileActivity state. This state is then passed down to my child component, ProfileActivity.js, where it should be display ...

"Want to know the best way to retrieve the most recent form input value using jQuery or Javascript

I've been reading a lot about using jQuery to set and get field values, but I'm encountering an issue where I can't retrieve the value after submitting, editing it in an input field, and then submitting again. It works the first time, but no ...

How does the AngularJS Dependency Injection system determine the names of the arguments it needs to inject?

Here is an example directly from the official website: function PhoneListCtrl ($scope, $http) { $http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); $scope.orderProp = 'age'; } The $s ...

Storing approximately 1 kilobyte of information throughout various pages

Is it feasible to store approximately 1kb of data while transitioning between two pages on the same domain using Javascript, Jquery (1.7), and Ajax? For instance, a user inputs data into a textbox on one page and then moves to another specific page. Can ...

Send form data using ajax technology

When testing my code on localhost, everything runs smoothly. However, when I tried running it on the server, it doesn't seem to be functioning properly. $("#MyUploadForm").ajaxSubmit(options); I would greatly appreciate any assistance with t ...

What is the most effective method for incorporating CSS using Javascript to target a specific aria-label attribute?

Is there a way to add a CSS property to a specific tag with a particular aria-label on document load? My goal is to change the flex order of a section to 2. I need help using JavaScript to target the aria-label 'block 1' so I can set the order t ...

What causes the URL to be undefined after making a JQuery Ajax POST request?

After performing an Ajax Post within a Profile View, I am attempting to refresh the view. Here is the code snippet: $.ajax({ url: '/Profile/Index', dataType: "html", type: "POST", data: JSON.stringify(10), success: ...

Charting data with missing values in Google Charts

I have generated a line chart using php and json to load the data. The issue I am facing is that the chart displays NULL values as 0, which does not look good. My assumption is that I may be formatting the json incorrectly, and what I really need is {"v" ...

Trouble fetching asynchronous data in Next.js Higher Order Component

Currently, I am in the process of creating a Higher Order Component (HOC) that will fetch user data from my API and then provide props to the wrapped component. This will allow the component to redirect the user based on their role. Although the HOC appea ...

Ways to display a price near a whole number without using decimal points

Currently, I am working on an ecommerce project where the regular price of an item is $549. With a discount of 12.96% applied, the sale price comes down to $477.8496. However, I want the sale price to be displayed as either $477 or $478 for simplicity. Yo ...