Difficulty concealing a div element using a basic JavaScript function

For a small project I'm working on, I created a div with some css styling, but I can't seem to hide it and I'm not sure why.

 var button = document.getElementById("button");
    button.addEventListener("click", open);
    var popup = document.getElementById("popup");

    function open() {
      popup.classList.toggle("hidden");
    }
* {
        box-sizing: border-box;
        padding: 0;
        margin: 0;
      }
      #popup {
        width: 15em;
        height: 20em;
        background-color: rgb(51, 50, 50);
        color: rgb(0, 159, 252);
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 1em;
      }
      .labels {
        padding: 0.5em 0;
      }
      .inputs {
        background-color: white;
        color: rgb (51, 50, 50);
      }
      .bottom {
        display: flex;
        flex-direction: row;
        justify-content: space-evenly;
        align-items: flex-end;
        width: 100%;
      }
      #cancel {
        color: red;
        position: relative;
        bottom: -5em;
        left: 3em;
      }
      #confirm {
        position: relative;
        bottom: -5em;
        left: 2em;
        color: rgb(0, 159, 252);
      }
      .hidden {
        display: none;
      }
<input type="button" value="OPEN" id="button" />
    <div id="popup" class="hidden">
      <h2>Add town</h2>
      <label for="name" class="labels"><span>Name</span></label>
      <input type="text" name="name" id="nameInput" class="inputs" />
      <label for="country" class="labels"><span>Country</span></label>
      <input type="text" name="country" id="countryInput" class="inputs" />
      <div class="bottom">
        <p id="cancel">Cancel</p>
        <p id="confirm">Confirm</p>
      </div>
    </div>

I've attempted to use the Chrome inspect tool, but no errors are showing up.

Answer №1

Your issue is related to CSS priority. The display:none property is being overridden by the higher priority display:flex rule within the #popup selector.

https://i.sstatic.net/YUlSj.png

To resolve this, you can use:

display:none !important;

Answer №2

The use of #popup{display: flex;} overrides the styling of your .class{display:none;}

Answer №3

Your div cannot be concealed due to the usage of display flex set by the id selector, which takes precedence over the hidden class. To address this, consider merging your hidden class selector with the id. Additionally, you may find this informative article on Specificity in CSS useful.

#popup.hidden {
  display: none;
}

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

Angular services Eclipse Mars JavaScript validation

When using Eclipse validator, it seems that the keywords "finally" and "catch" are not allowed: $http.get(url) .success(function (data) { // Handle data }) .error(function (data, status) { // Handle HTTP error }) .finally(function () { // Ex ...

When using AngularJS, the templateUrl feature delays the initialization of the controller, causing issues with dependent code

Recently, I began experimenting with AngularJS and so far, everything seems to be going smoothly except for one small issue. Let's consider a scenario where I have two directives, with one directive relying on the other, like this: angular.module(&ap ...

Getting the most out of option select in AngularJS by utilizing ng-options

I'm currently working with AngularJS and I am interested in utilizing ng-options for a select tag to showcase options organized under optgroups. Here is the array that I intend to use in my select option: $scope.myList = [ { "codeGroupComp ...

Toggle divs by using a checkbox (Vue.js)

On my authentication page, I have implemented a checkbox that I want to use to toggle between Sign Up and Sign In forms. When the checkbox is clicked, it should display the Sign Up form, and when it's unchecked, it should show the Sign In form. I fou ...

Photos failing to load in the image slider

Although it may seem intimidating, a large portion of the code is repetitive. Experiment by clicking on the red buttons. <body> <ul id="carousel" class="carousel"> <button id="moveSlideLeft" class="moveSlide moveSlideLeft"></button& ...

What is the best way to show an item in a list automatically when the cursor hovers over it?

Is it possible to display the options in a drop-down list without clicking on the menu? I have created a drop-down list using the "option" within the "select" tag, but I want the options to be visible without having to click. How can this be achieved? & ...

The has class method is failing to work within the if statement

I have encountered an issue with my Accordion code. When I test it, the word 'not' is displayed twice, while 'hve' is not displayed at all. $('.st-accordion li a').click(function() { if ($(this).parent().not('activeac ...

Adjust the visibility of components depending on the width of the screen

In my project, I have a Wrapper component that houses filters. This component becomes visible when the openMobileFilters() function is called and disappears when closeMobileFilters() is triggered. The Wrapper component occupies the entire page, and I want ...

Having trouble with Simplemodal showing link content in the modal window

Having trouble getting the modal window to display content from another link? I'm pretty sure I've connected them correctly using the right classes. This is the basic JavaScript provided by . jQuery(function ($) { // Load dialog on page load //$ ...

Turn off the Google Search Snackbar feature on your PWA for Android devices

Whenever I select text on my Progressive Web App that is installed and not running directly through Chrome, a snackbar pops up at the bottom of the screen displaying Google search results (at least on Android): https://i.sstatic.net/IbxfT.png Is there a ...

What causes the function execution to not be delayed by setTimeout?

function attemptDownloadingWebsite(link) { iframe = document.getElementById('downloadIFrame'); iframe.src = link; setTimeout(removeFile(link), 25000); } This is the remove file function: function removeFile(link){ $.ajax ...

Guide to transferring a JavaScript/Ajax variable to a PHP webpage with the help of AJAX

function initiateIperfSpeedRequest(speedVar, actualIp) { var xmlhttp = getAjaxObject(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { handleIperfResponse(xmlhttp.respo ...

Remove a picture from Cloudinary by utilizing the axios delete method

I am struggling to find a solution for deleting images on Cloudinary using axios delete request. It seems there is no specific API URL or code snippet available that can help me achieve this. Here is the code I am using to upload an image on Cloudinary: ...

Steps to generate interconnected fields within a schema

If I have a user schema with an inventory that contains multiple slots, how can I properly nest the slotimg and slottext fields inside the inventory field? For instance, like this: const UserSchema = new mongoose.Schema({ Inventory: { slot1img: { ...

The functionality is not operating correctly on Internet Explorer

This code is functioning properly in Mozilla and Opera, but it is not working in IE. Any assistance on this issue would be greatly appreciated. The JavaScript is working fine, however, in IE 10 only the back panel is displayed. Thank you for your help. h ...

Is there a way to rearrange html elements using media queries?

Here is a snippet of code showcasing how my website is structured for both desktop and mobile views. Display on Desktop <body> <div> <header> <div>example</div> <a><img src"my logo is here"/& ...

What is the best way to limit the length of a field in a datatable and provide an option to display the full data with "<more options>" at the end?

I am utilizing a datatable to showcase a report within my application. Some of the columns contain extensive data that I would like to truncate for viewing purposes, with the option to click and expand to see the entire content as needed. I attempted usin ...

Extracting a variable established on the client side and passing it to a server-side node backend

I am currently working on creating a comments section for a web application. The front end of the app displays information about the threading level of each comment. When users submit comments, I need to pass this threading information to the back end in o ...

Passing data between Service and Component using Angular Binding

I am attempting to show a loading indicator whenever an Http Request is sent. Although I am using Angular's Http Interceptor, the style of the indicator never seems to update. In my app.component.html, I have included a loading indicator. <div ...

Suboptimal placement of second submenu in Navbar

This question is inspired by a discussion on Stack Overflow that you can read here. Everything seems to be working fine, except when the 2nd level item is not at the top. In the example provided, the item with the 2nd level menu is the first item. If you m ...