Steps for designing a popup modal that triggers upon calling a specific function

I'm facing some challenges while trying to develop a modal that pops up when a specific section of an image map is clicked.

Here is the snippet from my HTML file:

 <head link rel="stylesheet" href="ryanspagecss.css"></head>
<body>
    <div class="main">
    <script src="lotrpage.js"></script>
    <map name="lotrmap">
        <area shape="poly" href="" coords="405, 50, 463, 80, 461, 141, 408, 173, 354, 144, 353,81" onclick="playerSelect()">
    </map>

    <div id="selectModal" class="selectModal">
            <p>Content inside the modal...</p>
    </div>

Below is the CSS code:

    #selectModal {
  width: 500px;
  overflow: hidden;
  background: pink;
  box-shadow: 0 0 10px black;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
  padding: 10px;
  text-align: center;
  display: none;
}

And here is the JavaScript part:

    function playerSelect(){
      //Show Modal
      let selectModal = document.getElementById('selectModal');
      selectModal.style.display = "block";
  
  }

I know I should change the display value from none to block in order to show the Div element, and this is what my JavaScript function aims to achieve. However, clicking on the image doesn't trigger any action. What could be missing in this implementation?

Answer №1

When utilizing the

document.getElementById('selectModal')
method, it searches for an element in your HTML with an id matching selectModal. However, in this instance, your modal has an id of selectOwner.

To address this issue, consider updating your code to

document.getElementById('selectOwner')

If you intend to locate the modal by its class name instead, you would need to use

document.getElementsByClassName('selectModal')
. Keep in mind that this will return a group of divs containing this class (technically an HTMLCollection, not an array)

Answer №2

The element you are trying to select with the ID selectModal does not exist in the document. To fix this, make sure you add id="selectModal" to your div.

Answer №3

After discovering that a commenter's code and a small snippet of my own code worked on JSFiddle, I decided to test if my CSS would successfully change the color of some random text... unfortunately, it didn't.

I soon realized that I had incorrectly linked my CSS file. Despite not receiving any syntax errors, I failed to notice this mistake.

Instead of

<head link rel="stylesheet" href="ryanspagecss.css"></head>

It should have been

<head> <link rel="stylesheet" href="ryanspagecss.css"> </head> 

Answer №4

Absolutely, you have a clear understanding of the difference between using display: "none" and display: "block".

After testing your code on my local machine, I found that nothing appeared on the screen.

To address this issue, there are two potential solutions:

  1. One option is to utilize the map & area tags with an image to create interactive elements in HTML. You can try it out by clicking here.

  2. Alternatively, you can insert a simple paragraph tag in your HTML code that will display text and trigger the modal when clicked. Insert the following code snippet into your HTML:

     <div onclick="playerSelect()">
         <p>
             Hi, Please click me to see the modal
         </p>
     </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

Having difficulty rendering image in vueJS

I am currently using the SideMenu component to display other SideMenuButton components, but I'm facing an issue where the image is not appearing. Here are the code snippets for both components: SideMenu: <template> <div id = "side-m ...

ASP.Net - Unexpected JSON Format Error

As I work on my ASP.Net web application, I am encountering an issue with binding data from a database to a Google Combo chart via a Web Service class. While I can successfully bind the data to a grid view, attempting to bind it to the chart results in the ...

Is it possible to sanitize user input without relying on !important?

Utilizing wp_editor, I have implemented a front-end form on my WordPress website for users to post without accessing the admin section. Although it is functional, there is a concern that users may copy and paste content with inline styles that could disrup ...

Customizing the appearance of different shapes using colors in three.js

I am having trouble changing the color of individual faces on a rotating cube in my code. Can anyone help me spot what is wrong? Much appreciated. $(function(){ var camera, scene, renderer, geometry, material, mesh; init(); animate(); ...

The issue lies in the transmission of video file data through the peer.js connection.send function

Creating a basic website where multiple users can access a specific dashboard using a unique id. When one user sends a video file, it should start streaming for other users on the same dashboard. I am implementing this functionality using Express.js, socke ...

Can the value of a key be changed to match the condition in a find() query when using Mongoose?

Having been well-versed in Oracle, I was thrown into a project requiring the use of a MongoDB database. Utilizing mongoose to connect to my MongoDB, my main query is whether it is possible to match a find condition before executing a query. For example, if ...

Retrieve a value using the jQuery each() method

Hello, I am a beginner in JavaScript and I need help with extracting values from JSON and adding them to an array. My goal is to be able to parse this array using another function later on. However, I'm struggling with returning the array after adding ...

A promise is given when a value triggers a function

Having a problem with my code in the second function. When I log inside the function, it works fine. But when I assign a variable and call the function, it returns a pending promise instead of true or false. const mongoose = require('mongoose') c ...

express/node: You are unable to define headers once they have already been sent to the client

I seem to be running into a problem with my expressJS application. Every time I try to post to a specific route, I keep getting the error message Cannot set headers after they are sent to the client. I've been troubleshooting this issue but can't ...

Safari is having trouble showing the webpage, although it is working fine on Internet Explorer, Chrome, and Firefox

After updating a client's website, I encountered rendering issues in Safari. Here is the link to the website: When testing the file on my Linux server with various browsers like Chrome, IE, and Safari, everything looked great without any font or layo ...

The error message in threejs is "GL_INVALID_OPERATION: Attempting to perform an invalid operation on

I'm having an issue when trying to combine post-processing with the "THREE.WebGLMultisampleRenderTarget." The console shows the error message: [WebGL-000052BE06CD9380] GL_INVALID_OPERATION: Invalid operation on multisampled framebuffer When using th ...

AngularJS ng-repeat is not updating when the state changes

Seeking assistance with an Angular application challenge I'm facing. I have implemented a ng-repeat in my app to display the latest messages stored in an array within a controller named "comunicacion": ng-repeat in comunicacion.html <div class=" ...

Can anyone suggest a more efficient approach to handling variations using CSS?

Within the message component, there are currently only two variants available: error and success. This project is built using vue3 script setup and utilizes SCSS for styling. <script setup lang="ts"> defineOptions({ name: 'Notificat ...

What steps are involved in integrating OpenCV into a JavaScript project?

After recently installing OpenCV via npm using this guide: https://www.npmjs.com/package/opencv I'm facing a simple question. How can I actually utilize the OpenCV library in my project? The site provides a face detection example code snippet: cv.r ...

How to Align Checkbox Buttons in a Bootstrap 4 Row

Trying to align checkboxes as buttons in Bootstrap is proving to be a challenge. The usual methods like "text-center" and "center-block" have been attempted within the surrounding divs and inputs but without success. (The unsuccessful attempts can be obser ...

Repositioning JQuery Autocomplete Results

I am currently integrating JQuery Autocomplete into my website, but I am encountering an issue with displaying the search results in the search form. The search form is placed within a Bootstrap 3 navbar on the main layout of the site. Embedded inside th ...

Obtain the outline shape in ThreeJS (duplicate border)

When working with a geometry, I often need to view it from different angles. For example, if I position my camera from the top, I want to be able to extract the outline of the geometry as a new shape. This allows me to then Extrude this shape further. In R ...

Resolving the issue of the 'Failed to load resource: net::ERR_FILE_NOT_FOUND' error

I'm encountering issues with loading my CSS in different browsers when using the "Copy Path" feature in VSCode. While everything displays correctly in preview mode within VS, none of the styles appear when pasting the path into Chrome or Edge. Below ...

Is there a way to synchronize the autohide duration for both the LinearProgress MUI and SnackBar components?

Can someone help me align my SnackBar with the LinearProgress so that they both have an auto-hide duration of 4 seconds? I've been struggling to figure it out for hours and haven't found a solution yet. Could the issue be in the useEffect part of ...

Accessing a text document from a specific folder

As a novice in the world of HTML and PHP, I am attempting to develop a script that can access a directory, display all text files within it, allow for editing each file, and save any changes made (all operations will be managed through an HTML form). Howev ...