Creating elements within the document using onClick

I have successfully created my own webpage and am now looking to incorporate some JavaScript into it. My goal is to have a functionality where clicking on a p tag within a div will trigger the appearance of a black div with approximately 70% opacity covering the entire page. On top of this overlay, an alert box containing text should appear that can be closed to make both the alert box and black div disappear. However, I am encountering issues in creating the black div using only HTML, CSS, and JavaScript without relying on jQuery.

Here is the HTML:

 <div class="varuhus">
     <p onclick="varuhusAlmhult()">Älmhult</p>
     <p onclick="varuhusStockholm()">Stockholm</p>
     <p onclick="varuhusMalmo()">Malmö</p>
 </div>

In the updated JavaScript section:

function varuhusAlmhult() {
    var backgroundDiv = document.createElement("DIV");
    document.body.appendChild(backgroundDiv);
    backgroundDiv.style.width = "100%";
    backgroundDiv.style.height = "100%";
    backgroundDiv.style.backgroundColor = 'black';
    backgroundDiv.style.opacity = .7;
}

Answer №1

  1. Start by creating an overlay element.
  2. Next, add a modal to the overlay.
  3. Append the overlay to the document's body.
  4. Remove the overlay upon clicking on it (excluding the modal).
  5. Proceed to style the overlay and modal using CSS.

You can also incorporate additional code to insert a close button if desired.

function displayModal() {
    var overlay = document.createElement("DIV");
    overlay.className = 'overlay';
    overlay.onclick = function() {
        overlay.remove();
    }
    var modal = document.createElement("DIV");
    modal.className = 'modal';
    modal.innerHTML = 'Some text here';
    overlay.appendChild(modal);
    document.body.appendChild(overlay);
}
html, body {
    padding: 0px;
    margin: 0px;
    height: 100%;
}
.overlay {
    background: rgba(0,0,0,0.7);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    font-size: 0;
    white-space: nowrap;
    text-align: center;
}
.overlay::before {
    content: "";
    height: 100%;
}
.overlay::before,
.modal {
    display: inline-block;
    vertical-align: middle;
    font-size: 1rem;
    white-space: initial;
    text-align: left;
    margin: 0 auto;
}
.modal {
    background: #FFF;
    border-radius: 5px;
    height: 80%;
    width: 80%;
    box-shadow: 0px 5px 15px -5px black;
}
<p onclick="displayModal()">Click me!</p>

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

Determining the Visibility of a Control within a Container with Scrollbars

Here is the code snippet that I am working with: <div style="overflow: scroll; width: 75px; background-color: Black; "> <table style="background-color: Red"> <tr> <td> < ...

Tips for positioning a Navbar in the middle of the screen at 90% width

I am struggling to center the navbar as a whole, rather than just the contents within it. Below is the CSS and HTML for my navbar. .navbar { border-radius: 10px 10px 30px 30px; background: #3CE18F; overflow: visible; position: fixed; bottom: ...

Navigating size measurements in percentages and pixels to achieve flawless alignment

Utilizing the calc property in a solution, I am facing challenges when trying to calculate the percentage width of an element with margins, padding, and borders set in pixels. Take a look at this demo: #form { margin: 200px auto; width: 360px; ...

Activate a jQuery plugin on elements that are dynamically generated

Here is the code snippet I am using: $(".address-geocomplete").geocomplete({ country: "US", details: ".details", detailsAttribute: "data-geo" }); When these elements are loaded with the document, everything works smoothly. However, there seem ...

The second function in Vue.js was unable to initialize the data field within data() due to a missing call for assistance

I have very little experience working with vue js. There are two functions that I am using: loadComponentsOfUser() and loadUserId(). The loadComponentsOfUser() function depends on the userID field being loaded by the loadUserId() function. data() { retu ...

Having trouble with JavaScript arrays returning undefined within a for loop?

When I use a for loop to iterate through this array, it returns undefined in updateField(this.value, vehicles[i]+"_brand" However, $("#"+vehicles[i]+"_year").change(function(){ Correctly retrieves the values from the array. Why is this happening and ...

How to create a responsive image that appears over a button when hovering in Bootstrap?

My goal is to display an image over a button when hovering. I've tried adding the .img-responsive class in Bootstrap while including the image in the HTML, but it's not working as expected with my current method of loading images. The buttons the ...

Ensure that the ::after element matches the size and position of a flex child element

I've been exploring ways to enhance the box-shadow effect on my flex elements, taking inspiration from this insightful article. Unfortunately, I'm running into issues as the implementation is not functioning correctly. Here's a demo of the ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

Unable to invoke a mixin function within a JavaScript function

I've been experimenting with a small project utilizing nuxt.js, and currently, I've developed a plugin using mixins as shown below. However, I'm puzzled as to why function b is not working inside the render function: import Vue from 'v ...

Selecting a single checkbox automatically selects all other checkboxes

Is there a way to automatically check all other checkboxes if one checkbox is checked? Here's the code snippet I have: <tr> <td class="small"><asp:CheckBox ID="chkReportModuleName" runat="server" name="report"></asp:CheckBox& ...

Design a user-friendly pdf form feature where selecting one checkbox automatically triggers the selection of other boxes

Upon viewing the image below, you will notice a checkbox labeled Long Term Care on the left side. My goal is to have this box automatically check the other boxes that are in bold. Being new to this process, I am unsure of where to begin. I assume that I ne ...

Sending data to functions

Hello all, I'm a beginner with Vue so please bear with me :) I am facing a challenge where I have a function that retrieves user attributes and based on those attributes, I need to run a GraphQL query in another function. Both functions are under the ...

Obtain content from a div element using jQuery

Here is a snippet of HTML code that I am working with: <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> </head> <body> <script src="jquery.js"></script> <scri ...

Struggling with managing multiple checkboxes in React with a single onChange event handler not functioning

I am currently working on a project that involves multiple checkboxes. My goal is to use one onChange function to keep track of the checked boxes, store their values inside an array, and remove them when unchecked. However, I seem to be encountering an err ...

Having problems with jQuery's document.on hover toggle method?

Recently, I've been trying to implement a jQuery function that toggles an image when hovering over an element and toggles it back when exiting the element. However, I encountered a problem when using the $(document).on selector. I attempted to use $(d ...

Using jQuery to select the input value and append content, then utilizing the same input value as the added content

I need help with a task involving two columns. I would like to select either column 1, column 2, or both to add content to using checkboxes. My approach is to use the "value" attribute of the checkbox as a $selector. After selecting the columns, I want to ...

Using TypeScript: Implementing array mapping with an ES6 Map

If I have an array of key-value objects like this: const data = [ {key: "object1", value: "data1"}, {key: "object2", value: "data2"}, {key: "object3", value: "data3"}, ] const mappedData = data.map(x => [x.key, x.value]); const ES6Map = n ...

Utilize the nest function in D3 to organize flat data with a parent key into a hierarchical structure

I'm searching for an elegant and efficient solution to transform my input data into a hierarchical structure using d3.js nest operator. Here is an example of the input data: [ {id: 1, name: "Peter"}, {id: 2, name: "Paul", manager: 1}, {id: 3, name: " ...

Enhancing MongoDB performance with Mongoose for efficient array save or update operations

After a user saves a question, the question may include a list of "tags". To handle this data efficiently, I need to compare these tags with existing ones in the collection. If a tag already exists, its count should be updated; otherwise, it needs to be ...