JS click event listener is failing to activate the function as intended

In an attempt to create a modal window, I am facing issues with adding a click event listener to a div that should modify the CSS style of another element. The goal is to change the display property from 'none' to 'border-box' when the div/button is clicked, but for some reason, clicking the div does not seem to have any effect. Even after console logging, there were no visible results. Interestingly, moving the clickable div to a different part of the HTML document seems to resolve the issue, which has left me feeling perplexed.

Below is the provided snippet of HTML:


    <!DOCTYPE html>
<html lang="en">
...
</body>
</html>

Accompanied by the JavaScript code snippet:


document.querySelector('#modal-btn').addEventListener('click', 
  function() {
    document.querySelector('.modal').style.display = 'border-box';
  });

And here's a sample of the corresponding CSS:


* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Titillium Web, Arial, sans-serif;
    font-size: 1rem;
}
...

Answer №1

The border-box property does not apply to the display property, as David pointed out, border-box is used for box-sizing. Your JavaScript code works perfectly fine. I have simplified it and created a snippet for you:

document.querySelector('#modal-btn').addEventListener('click',
  function() {
    console.log('working');
    document.querySelector('.modal').style.display = 'block';
  });
.modal {
  display: none;
}

#modal-btn {
  cursor: pointer;
}
<section class="section-background projects-color">
  <div class="projects-container container">
    <h2 id="projects-section">Projects</h2>
    <div class="section-divider"></div>

    <div class="project">
      <h3>E-commerce Site</h3>
      <div class="project-image project-one"></div>
      <p class="project-description">Lorem ipsum dolor sit amet, pri audire deterruisset ut, mei eu stet diceret pertinacia. Has salutandi mediocritatem te, copiosae disputationi conclusionemque in pro, fugit eleifend argumentum quo et. Sea te quidam instructior complectitur, est vidisse
        consequat deterruisset ex.</p>
      <div class="project-icons">
        <a href="https://github.com/catspacesuits/new-portfolio"><i class="fab fa-github project-btn"></i></a>
        <div class="project-btn" id="modal-btn">More details</div>
        <div class="project-btn">Visit site</div>
      </div>
    </div>
    <div class="modal"></div>
    <!-- Modal window -->
  </div>
  <!-- end of project container -->
</section>

I updated display property from border-box to block and it now works correctly. Additionally, I included a console log to confirm that the function executes when the button is clicked.

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

How to properly implement ng-if for a select option in Angular.js?

Within the controller, I defined a scope variable: $scope.readOnly = true. In the HTML code: <select ng-if="readOnly" ng-model="readOnly" required> <option value="true" selected>True</option> <option value="false">False ...

Achieve sliding animations with Pure CSS using slideUp and slideDown techniques

Currently, I have implemented a code snippet to showcase a menu along with displaying submenus upon hovering over the main menus. Now, I am looking to introduce some animation effects for the submenus when they appear, similar to the slideUp and slideDown ...

Leverage the sync function within the await .then("sync") method in JavaScript

If a synchronous function is called within the .then part of an asynchronous await .then, such as: await asyncFunc(...) .then(res => sendMSG(res)) .catch(err => next(err)); function sendMSG(res){ xyz } The sync function sendMSG i ...

Leveraging a factory value within another factory in AngularJS

Greetings! I am currently working on a web application using AngularJS. Within my project, I have a JavaScript file containing two factories that make HTTP calls to a web API. My goal is to utilize the output of one factory within another factory. Below is ...

Activate the button in React after ensuring all form fields are populated

Hello everyone, I'm new to using React and I'm trying to implement some basic validation but I'm struggling. Here are my goals: Disable the button if any form fields are empty Enable the button only when ALL form fields are are filled I w ...

What is the best way to change an image using JavaScript?

Is there a way to change an image when the tag doesn't have an id? <img src="URL_1" /> I need to replace URL_1 with URL_2 Any suggestions on how to do this? ...

The small device screen in Bootstrap 4.5 is experiencing an issue where two rows are overlapping

I recently started learning Bootstrap and encountered an issue. When resizing the browser screen to a smaller size, I noticed that the rows within separate containers were overlapping. Screenshots have been provided for reference. Can someone please guide ...

Weirdly enough, Angular's $uibModalInstance.close() function doesn't seem to actually close the modal

In my project, I have a modal that prompts the user to enter some input. The logic of the code is structured as follows: When the user clicks on the 'Add' button, a modal window appears asking for input. After entering the input, the user clicks ...

What techniques can I use to generate a 3D visual effect by shifting the background image layer as the user scrolls (creating a Parallax effect

I have combined two images in the heading; one on top of the other. My goal is to create a subtle vertical movement in the background image as the user scrolls, giving the illusion of depth. Both images share the same width, but the background image is tal ...

concealing directory titles within HTML and AJAX

What are the potential security risks associated with exposing PHP folder names? If there are risks, what methods can be used to conceal the folder names within html hyperlinks and ajax code? ...

Combining both responsive and nonresponsive code on a single webpage

The project I am currently working on is a massive website that we cannot tackle all at once. As a result, we are developing it in phases. The challenge we are facing is transitioning from a 1990s table-based structure to a modern Bootstrap-divs responsive ...

Animating the scaling of a shape using Three.js

Currently, I am embarking on my JavaScript coding journey and experimenting with Three.js as well. I have created a scale animation for a geometry in order to achieve a subtle breathing effect using the following code: var frame = 0; function animat ...

How do you write functions in ES6?

When attempting to access my namespaced store in Vue, I encountered an issue: methods: { ...mapActions('Modal', [ 'toggleActive', ]), close: () => { this.toggleActive(); } This resulted in the follow ...

Restrict the character count in tinyMCE

My project uses the tinyMCe editor and everything is functioning properly. However, I now need to limit the number of characters that can be inserted into the tinyMce textarea. tinyMCE.init({ // General options mode : "textareas", theme : "simple", plugin ...

What causes the disparity in functionality between CSS's Left 75% and Right 25% properties?

Here is my CSS code snippet: .bloodparam > .highvalue { bottom: 12px; right: 25%; } and .bloodparam > .highvalue { bottom: 12px; left: 75%; } I expected the element to be in the same position with both lines of code, but I'm noticing differe ...

Inserting JQuery Selector from a .js file into a .php file at the beginning

Incorporated in my project is a jquery plugin for a slider, which includes the code snippet below for handling 'next and prev' buttons. $(selector).prepend('\ <ul class="mk-nav-controls">\ <li>& ...

Could you please guide me on resolving the Blazor/Bootstrap 5 CSS problem related to styling "striped" tables?

Looking for a solution to fix a CSS issue involving Bootstrap 5 "table-striped" and displaying Blazor Razor components. In my Blazor/Bootstrap 5 page, I have a table displaying content fetched from a service at runtime. Initially, the Bootstrap CSS is vis ...

Show MySQL SELECT output on a HTML page

I have created a button that opens a new .html page in a new browser tab, allowing users to input data that is then stored in a MySQL database using a PHP script. Now, I am looking for a way to display the content of my MySQL database on my index.html fil ...

Disconnection between client and server communications

Can communication between two entities be established in this scenario? For example, if a server receives a file upload and determines it is incorrect, can it send an event to the client? The client would then catch the event and manipulate the DOM to dis ...

Select a different radio button to overwrite the currently checked one

I am facing a situation where I have two radio buttons. The default checked state is one of them, but I want to change that and make the other one checked by default instead. Here is the HTML code: <div class="gui-radio"> <input type="radio" v ...