Step-by-step guide to showing a div upon clicking an element using jQuery

I am facing a challenge with an invisible div that I need to make visible upon clicking an element. However, my current approach does not seem to be working. Can you help me figure out what I am missing?

I attempted to target <a class=".demo"> using jQuery and the click function to add the class .open to <div class="demo-div"> in order to make it visible.

$(".demo").click(function() {
  $(".demo-div").addClass("open");
});

$(".demo").click(function() {
  $(".demo-div").removeClass("open");
});
.demo-div {
  background: #3AB0E0;
  color: #18191D;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  padding: 3.75rem;
  z-index: 99;
  opacity: 0;
  visibility: hidden;
  overflow-x: hidden;
  overflow-y: hidden;
  transform: tranlateY(-100%);
  transition: all 0.5s ease-in-out;
}

.demo-div.open {
  opacity: 1;
  z-index: 99;
  visibility: visible;
  transform: translateY(0);
  transition: all 0.5s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
  <ul>
    <li><a href="javascript:void(0);" class="demo">click here</a></li>
  </ul>
</nav>
<div class="demo-div"></div>

Despite my efforts, the class open is not being added to demo-div

If the class demo-div is removed, the div becomes visible

opacity: 0;
visibility: hidden;

***Update I realized my mistake - when adding the new class, it should include a closing button such as .demo-close-btn

$(".demo").click(function() {
  $(".demo-div").addClass("open");
});

$(".demo-close-btn").click(function() {
  $(".demo-div").removeClass("open");
});

Answer №1

Opt for toggleClass() over add or remove methods.

$(".example").on('click', function() {
  $(".example-container").toggleClass("expand");
});

Answer №2

The issue arises from having two conflicting event handlers attached to the same element, both trying to add and remove a class.

To resolve this, it is recommended to utilize a single event handler that toggles the class based on its current state using toggleClass():

$(".demo").click(function() {
  $(".demo-div").toggleClass("open");
});

However, it should be noted that when the open class is applied to .demo-div, the .demo element becomes unclickable as it is covered. To address this, another click handler can be added to .demo-div to remove the open class like so:

$(".demo").click(function() {
  $(".demo-div").addClass("open");
});

$('.demo-div').click(function() {
  $(this).removeClass('open');
});
.demo-div {
  background: #3AB0E0;
  color: #18191D;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  padding: 3.75rem;
  z-index: 99;
  opacity: 0;
  visibility: hidden;
  overflow-x: hidden;
  overflow-y: hidden;
  transform: tranlateY(-100%);
  transition: all 0.5s ease-in-out;
}

.demo-div.open {
  opacity: 1;
  z-index: 99;
  visibility: visible;
  transform: translateY(0);
  transition: all 0.5s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
  <ul>
    <li><a href="javascript:void(0);" class="demo">click here</a></li>
  </ul>
</nav>

<div class="demo-div"></div>

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

Function starting too slow due to rapid Loading Spinner Image display

I am struggling with a function that is supposed to display the contents of posts when clicked on. My goal is to have a loading spinner appear for a few seconds before the post content shows up. However, I am facing an issue where the spinner only appears ...

Using Angular/Typescript to interact with HTML5 Input type Date on Firefox (FF)

Are there any Angular/Typescript projects that are completely built without relying on third-party libraries? I am encountering problems with Firefox and IE11. It works fine on Chrome where the value can be read, but the calendar does not display when us ...

The ashx file is triggered with every server control postback in jqgrid

I have an asp .net webforms page with a jqgrid, as well as several other server controls. The jqgrid is populated using an ashx file which retrieves data from the database and binds it successfully. Challenge Whenever a postback occurs (such as from a dro ...

When selecting an option from jQuery autocomplete, the suggestions data may not always be returned

I have a question about the jQuery autocomplete feature. In my code snippet below, I have an input with the ID aktForm_tiekejas that uses the autocomplete function: $('#aktForm_tiekejas').autocomplete({ serviceUrl: '_tiekejas.php', wid ...

Tips for implementing varying styles depending on the number of columns in a table

I am working with multiple tables that share similarities, but differ in the number of columns each table has. I am looking to create a styling rule where depending on the number of columns, the width of each column will be set accordingly. For example, if ...

What is the reason for the disparity in the way a form type is displayed?

Two versions of a small project exist, 'webpack' and 'retro'. The webpack version utilizes ../app.scss/...@import "~bootstrap/scss/bootstrap"; for serving styles (css), while the retro version uses the outdated <link rel=&q ...

Ending the loop of a jQuery JSON array when reaching the final row

I have a function that retrieves a JSON array and shows the results as a list with ten items. However, if there are fewer than ten results, it starts over from the beginning. This is my code: $.ajax({ url: 'http://www.entertainmentcocktail.com/c ...

Dealing with interstitial advertisements on mobile devices: What is the best approach for handling zoom?

I am facing a challenge with displaying interstitial ads on mobile sites. Different publishers may have varying viewport settings and initial scales on their mobile sites, making it difficult to ensure that the ad appears correctly on all devices with the ...

Questions about clarifying JS promises and async-await functions

After doing some reading on promises in JavaScript, I have come across conflicting information which has left me with a few basic questions. I have two specific questions that need clarification: Is it necessary for every function in JavaScript to be ca ...

Ways to access the value of an input field using Javascript

I am currently utilizing the valums-file-uploader plugin for file uploads via ajax. However, I have encountered an issue with its functionality. Below is the script that I am using: <input type="text" id="Gaurav" name="Gaurav" /> <script src="fil ...

Insert a new class within the container div

I am looking to insert a 'disabled' class within a parent div named 'anchorxx' https://i.sstatic.net/3KRMQ.png The disabled class div can be located anywhere within the anchorXX divs Is it achievable using jquery? I am unsure how to ...

What is the best way to ensure that my Material UI container occupies the entire width and height of the

Struggling to fit my content within a Material UI container in a React project, but it's creating odd margins. Check out the current look: https://i.stack.imgur.com/TaQs2.png Here's how I want it to display with full width: https://i.stack.imgur ...

Display link based on checkbox selection using either Jquery or PHP

My website features a Document Request form where users can provide their basic information and select the specific documents they are interested in receiving. The form includes fields for Name, Email, and Documents Requested including Document 1, Documen ...

What is the best way to conceal a div when there are no visible children using AngularJS?

I need help with hiding a parent div only if all the child items are invisible. I have successfully hidden the parent when there are no children, but I am struggling to figure out how to hide it based on visibility. <div ng-repeat="group in section.gro ...

Utilizing CSS to place an image on top of the upcoming <div> container

My goal is to create a layout similar to the one displayed in the linked image. https://i.stack.imgur.com/7DSE9.jpg Currently, I am using Bootstrap 3 and my HTML markup looks like this: <body class="gray-bg"> <section class="white-bg"> <d ...

Sharing Variables with $(document).ready

I need help understanding why this code is not functioning and how I can fix it. Despite my efforts to use namespaces and IIFEs, I am still unable to make it work. $(document).ready(function() { alert (hi); }); $(document).ready(function() { var hi = ...

Navigating to the following webpage with Selenium using Python

url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292" This particular website features a comment pagination at the bottom of the page. Upon inspecting the page, I noticed that the buttons were structured like this: &l ...

Enhancing the appearance of individual cells in jQuery DataTables

While working with jQuery DataTables, I have successfully managed to access row data and apply styling to the entire row. Below is the code snippet used to initialize the datatable: var $dataTable = $('#example1').DataTable({ "data": data, ...

CSS: Revert INPUT Element Back to Default Width

Although it may seem impossible, I am still going to ask the question. Suppose I have the following HTML: <div style="width: 500px;"> <input class="full" /> </div> And the corresponding CSS: input { width: 100%; } .full { ...

Eliminate any vacant areas within a container and shift the container, along with its contents, on the webpage

I want to eliminate the empty space within the container while maintaining some spacing around the black box, and ensure responsiveness. Additionally, how can I shift the container with its content downwards and to the right of the webpage, while still ke ...