Looking to show a div upon clicking a link with the use of Javascript

Looking for a workaround due to restrictions on using alert or any Js dialog box, I need to create some sort of magic trick:

1. Create a div with a link named "info". 2. Develop an invisible div that will serve as my "PopUp" containing random information. 3. When the info link is clicked, the invisible div should appear.

Although this may seem simple and fairly straightforward, I'm struggling to grasp the logic behind making it work. Just to save myself from embarrassment, I recently started programming... about a month ago and have only dabbled in HTML and CSS so far. JavaScript is still new territory for me.

This is the code I currently have:

<div class="scroll-area" id="lista">
    <div class="box">
        <p>Item #1</p>
        <p class="info"><a href="#" id="lnkInfo">Info</p>
        </a>
        <p class="info"><a href="#">Take</p>
        </a>
    </div>
    <div class="box">
        <p>Item #2</p>
        <p class="info"><a href="#">Info</p>
        </a>
        <p class="info"><a href="#">Take</p>
        </a>
    </div>
</div>

Now let's unveil the mysterious "PopUp"...

<div class="popUp">
    <ul>
        <li>BLA BLA</li>
        <li>BLA BLA/li>
        <li>BLA BLA</li>
        <li>BLA!</li>
    </ul>
</div>

In case you need the CSS too:

.popUp {
    position: absolute;
    width: 300px;
    height: 300px;
    margin: 0 auto;
    background-color: #ecf0f1;
    top: 100px;
    left: 100px;
    display: none;
}

.show {
    display: block;
}

And here's a snippet of what could be the starting point for the JS:

var elementPopUp = document.getElementById('lnkInfo');

elementoPopUp.addEventListener('click', validate);

function validate() {
    document.getElementById('popUp').className += ' show';
}

Answer №1

UPDATED: There are syntax bugs in your HTML code that need to be fixed. You are using getElementById to get an element, but you have set popUp as a class instead of an id.

<div class="scroll-area" id="lista">
    <div class="box"> 
        <p>Item #1</p>
        <p class="info"><a href="#" id="lnkInfo">Info
        </a></p>
        <p class="info"><a href="#">Take
        </a></p>
    </div> 
    <div class="box">
        <p>Item #2</p>
        <p class="info"><a href="#">Info</a></p>

        <p class="info"><a href="#">Take</a></p>
    </div>
</div>
<div id="popup" >
    <ul>
        <li>BLA BLA</li>
        <li>BLA BLA</li>
        <li>BLA BLA</li>
        <li>BLA!</li>
    </ul>
</div>

CSS:

#popup {
    position: absolute;
    width: 300px;
    height: 300px;
    margin: 0 auto;
    background-color: #ecf0f1;
    top: 100px;
    left: 100px;
    display: none;
}

#popup.show {
    display: block;
}

Javascript:

var elementPopUp = document.getElementById('lnkInfo');

elementPopUp.addEventListener('click', validate);

function validate() {
  var d = document.getElementById("popup");

  d.classList.add("show");
}

Answer №2

If you follow these steps:

<div class="scroll-area" id="list">
         <div class="box">
            <p>Item #1</p>
            <p class="info"><a href="#" id="lnkInfo" onclick="validate()">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>
        <div class="box">
            <p>Item #2</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>
              </div>

And update the javascript function to this:

function validate(){

document.getElementById('popUp').style.visibility = 'visible';
 }

This should solve the issue. Please note that I have not tested it, so there might be some syntax errors.

Answer №3

Learn how to manipulate element styles using JavaScript with this practical example:

HTML Code

<button id="shower">Show</button>

<br>
<button id="hider">Hide</button>

<br>
<div id="popup" class="hidden">Hello</div>

JavaScript Code

document.getElementById("shower").addEventListener("click", function (e) {
    var myDiv = document.getElementById("popup");
    myDiv.classList.remove("hidden");
    e.preventDefault();
});

document.getElementById("hider").addEventListener("click", function (e) {
    var myDiv = document.getElementById("popup");
    myDiv.classList.add("hidden");
    e.preventDefault();
});

CSS Style

.hidden {
    display: none;
}

Answer №4

Check out this simplified approach:

The HTML:

<button id="showButton">Show Content</button>

<div id="contentToShow">Content to be shown</div>

The CSS:

#contentToShow {
    display: none;
}

The JavaScript:

document.getElementById("showButton").onclick = function() {
    document.getElementById("contentToShow").style.display = "block";
};

Feel free to visit the JSFiddle link for a demonstration.

Hope that clears things up!

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

Navigating the issue of "Experiencing additional hooks rendered compared to the previous render"

I'm currently in the process of developing a small application where certain elements will be nested within one another. My approach involves segmenting each component into its own file (children), returning a function with two components in each Rend ...

Guide to making a dynamic image map with interactive links using HTML and CSS

Seeking advice on how to create clickable areas within an image that lead to different pages on my website. Image maps seem like a good solution, but the issue is that they require specific coordinates which may not work well for responsiveness. I was hop ...

What is the best way to position a div at the bottom of the main div?

How can I position a div with the class "boxLinks" at the bottom of the main box with the class "main-box"? Here's my idea: The main class has a width of 1200px; within this main class, there are 5 divs with each div having a width of 25%. .main- ...

The AngularJS script is throwing an error that states "ReferenceError: Invalid left-hand side in assignment

While attempting to establish a connection with a webservice, I made an attempt using AngularJS $http option. However, when testing it out, I encountered an error in the console of my Chrome browser: ReferenceError Invalid left-hand side in assignment. Des ...

Sort various divs using a list

I have multiple divs containing different content. On the left side, there is a list of various categories. When a category is clicked, I want to display the corresponding div for that category. Initially, I want the main category to be loaded, with no opt ...

Arrange css3 menu circles to be displayed next to each other

I have managed to figure out most aspects of these circles except for how to arrange them side by side. Currently, they are stacked on top of each other. I have successfully determined the colors, fonts, text positions, and other details with some assistan ...

Is the append() function malfunctioning in jQuery?

Why is it copying two lines when I only want one line to be cloned on button click? Is there a way to make sure that only a single line is copied each time the button is clicked? Here is my code: $(function() { $('.add-more-room-btn').clic ...

What is the process for retrieving my dates from local storage and displaying them without the time?

Having an event form, I collect information and store it in local storage without using an API. However, I face a challenge when extracting the startdate and enddate out of localstorage and formatting them as dd-mm-yyyy instead of yyyy-mm-ddT00:00:00.000Z. ...

Utilize the useRef hook in React to enable copying text to the clipboard

Looking to implement a copy to clipboard functionality in React using the useRef hook? Want to accomplish this without relying on any additional libraries? Take a look at my code snippet below. Currently, I'm encountering an error stating myRef.curren ...

Creating a new component when a click event occurs in React

Currently diving into the world of React while working on a project that involves mapbox-gl. I'm facing an issue where I can successfully log the coordinates and description to the console upon hover, but I can't seem to get the popup to display ...

Vue component updating its model only upon input element losing focus

I'm a beginner with vue and I'm currently working on incorporating an ajax search feature that triggers when a keyup event occurs. I have noticed that the model only updates when the input element loses focus. Sample HTML Code: <input name=" ...

Configure your restify REST API server to handle both HTTPS and HTTP protocols

I'm currently utilizing node.js restify version 4.0.3 The code snippet below functions as a basic REST API server supporting HTTP requests. An example of an API call is var restify = require('restify'); var server = restify.createServer( ...

Steps for preventing form submission when the username is unavailable

After checking the availability of the user name, I encountered an issue where the form still gets submitted even if the username is not available. Updated Code: <SCRIPT type="text/javascript"> $(document).ready(function(){ $("#emailch").change(fu ...

Using Angular 2 to execute an interface while making an HTTP GET request

I've managed to successfully retrieve and display data from a JSON object using *ngFor in Angular. However, I am struggling with applying an interface to the retrieved data. This is the content of my interface file: import {Offer} from './offer ...

Combining rows and columns in Flexbox design

https://i.stack.imgur.com/tDLii.png I'm able to easily create this layout using the float property, but I'm having some difficulties achieving the same with flexbox. CSS: .a { background: red; float: left; width: 30%; ...

Guide on dividing and presenting ajax information into two separate div containers

I am attempting to showcase two sets of data on separate divs using ajax. Below is the code I am utilizing for this task. Here is the ajax implementation $(function () { $(".myBtn").click(function () { var id = $(this).data("id"); ...

Error encountered when extending Typography variant in TypeScript with Material UI v5: "No overload matches this call"

Currently, I am in the process of setting up a base for an application using Material UI v5 and TypeScript. My goal is to enhance the Material UI theme by adding some custom properties alongside the default ones already available. The configuration in my ...

Calculating the number of rows using JQuery

After browsing similar questions on Stack Overflow, I have not found a solution that fits my specific case. I have multiple tables on a page that share the same template without unique IDs. I am trying to determine if the rows are empty when certain data i ...

Top-notch java tool for caching and minifying dojo, jquery JavaScript, and CSS files

In our project, we have downloaded the Dojo and jQuery libraries from Google CDNs. I am currently seeking a Java tool that can both cache and minify these libraries. The caching process should take place within the ROOT project of Tomcat. While I am awar ...

I am experiencing an issue with the display inline feature not functioning properly in Firefox

I am working with Html code: <div class="login"> <form class="form-horizontal signin"> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">Ema ...