Only the first element is responding to the button onclick event

When I click the button on the elements, the overlay box only works with the first one and not the rest.

I have already tried adding two classes but it doesn't seem to be working. I read that this might be the issue, but I am struggling to make it work properly.

<div class="container">
<input type="button" value="Contact Now" id="Overlay" class="overlay"  
/> 
</div>

<div id="ogreModel" class="modalbox ogrebox" >
<div class="dialog">
<button title="Close" onClick="overlay()" class="closebutton" id="close">close</button>
<div style="min-height: 150px;">
    </div>
    </div>
</div>


<script>
// JavaScript code for functionality
document.getElementById("Overlay").addEventListener("click", function(){
var elem = document.getElementsByClassName("modalbox");

elem[0].style.display = 'block';

}) ;
document.getElementById("close").addEventListener("click", function(){
var elem = document.getElementsByClassName("modalbox");
elem[0].style.display= 'none';
});
</script>

What exactly needs to be changed in this code so that the rest of the elements display the box after clicking the button?

Answer №1

There is no need for the onClick="overlay()" attribute on your close button since you are already assigning a click event listener in your script.

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

Using JavaScript, you can manipulate the position of a line on a canvas to

I want to create a feature where users can manipulate lines on a canvas by skewing them. This means they would be able to drag one end point of the line to a desired point on the same x-axis using JavaScript and HTML5 canvas. Can someone please provide g ...

The Highcharts download feature is not available when the title is removed

After setting the title of my chart to null, I noticed that I am no longer able to access the download menu on the chart. Check out this example for reference: http://jsfiddle.net/JVNjs/954/ var chart = new Highcharts.Chart({ chart: { renderT ...

Error: The value for $regex must be in the form of a string

Encountering a problem with an error in the Console: { [MongoError: $regex has to be a string] name: 'MongoError', message: '$regex has to be a string', waitedMS: 0, ok: 0, errmsg: '$regex has to be a string', code ...

What is preventing me from being able to access a property within my function?

In the post method below, I am trying to access baseUrl. However, it is showing undefined. Can you help me understand why and provide a solution? const API = { baseUrl: "http://my_api_address", post: (path, payload) => { let headers = { ...

The compilation of the Electron/Angular application encountered an error due to the absence of the necessary http

I have been tasked with adding a feature to an existing Electron/Angular application that has not been worked on for some time. When I try to launch the app in development mode using ng serve I encounter the following error message: Your global Angular ...

What is the best way to locate a particular item in the JSON file for Our World in Data's COVID-19 Master Vaccinations dataset?

As a new coder, I am currently working on accessing specific elements within the COVID Vaccine JSON file from Our World in Data. However, I'm struggling with the syntax of my code and the structure of the JSON file. I have managed to retrieve data fr ...

What is the best way to iterate through an object and retrieve the following 3 items using a for loop in JavaScript

After fetching data from a website and storing it in a variable, I have successfully implemented a for loop that returns 3 properties from the object every time a button is clicked. However, the issue arises as the same data is returned with each button c ...

Unable to link to '' because it is not recognized as a valid attribute of '' in Angular 2

I encountered an exception while working on my Angular 2 project and I'm struggling to figure out the cause. Below is the snippet of my code: ts: import {Component} from "@angular/core"; import {GridOptions} from "ag-grid"; import {RedComponentComp ...

Ways to update text randomly without the need to refresh the page

Currently, I have a script that provides users with a random word. However, the only way to receive a new word is by refreshing the page... I would like to be able to set the words to change at specific intervals rather than needing to reload the page or ...

What is the process for displaying HTML page code received from an AJAX response?

My current project involves implementing JavaScript authentication, and I have a specific requirement where I need to open an HTML file once the user successfully logs in. The process involves sending an AJAX request with the user's username and passw ...

Refreshing the page when the hide/show button is clicked with jQuery

I have a situation where I am using jQuery to toggle the visibility of a div by clicking on show/hide buttons. However, I am facing an issue where the code does not work as intended. Every time I click the buttons, the div reverts back to its original stat ...

Attempting to hide anchor tags for "register" and "login" while making the anchor tag for "logout" visible after signing in

After signing in, I want the register and login anchor tags to disappear and the logout anchor tag to appear. However, the login and register links stay visible on the page even after I sign in. <div class="navbar-nav ms-auto"> <% ...

Unable to send messages despite successful connection through Sockets.io

My Java Server is set up to listen for messages from an Ionic 2 Client using Sockets.io. The server can successfully send messages to the client, but I am facing issues with getting the client to send messages back to the server. For example, when the jav ...

Transferring data between two TypeScript files of different Angular 2 components

I have two components, Component A and Component B, which are not parent-child components but I need to pass data from Component A to Component B. Example: Component A.ts has an array of data: myData: [ {'name':'some a','email& ...

Rails 5 not allow user submission of bootstrap modal form

I am facing an issue with a form inside a modal in my Rails application. When I click on the submit button, nothing happens. How can I use Ajax to submit the modal form and save its content on another page? <button type="button" class="btn btn-primary ...

Reyanpapa's guide on form validation with Javascript

Hello there, I recently ran into an issue with my HTML and JavaScript program. Whenever I enter input data in the name, email, and age fields, it triggers an error message asking me to select a gender. But after receiving the error message, the informati ...

Why does the array format of the values saved in app.locals seem to disappear when they are accessed in the view?

I have created a date values array and stored it in an app.locals variable using the code snippet below: prepDataList.forEach(function(item){ var str = item.createdAtDate.toDateString(); //str = str.substring(4, 10); labelsArray.push(str); counts ...

JavaScript: unable to locate information within JSON reply

Looking to develop a Twitter bot utilizing the Twitter and Spotify APIs. Making progress, but encountered an issue I can't tackle alone: Seeking the artist and song name of the top 1 track from the top 50 Spotify songs. Upon sending a request to the ...

Three.js ShadowMap Tutorial

Currently working with three.js version r73 and encountering two issues regarding shadowmaps. Initially, when I toggle the 'logarithmicDepthBuffer' setting to true for my renderer, shadows no longer appear. Additionally, I am unable to see shad ...

Implement Spacing Between Components in React Native

Seeking assistance in resolving the issue of overlapping views within my react native app, specifically requiring spacing between them. Upon clicking the plus sign twice in the top right corner, the two Views overlap without any space between them (referr ...