Steps for showcasing each element of an array separately upon a mouse click

I am working on a website where each click on the screen reveals a new paragraph gradually. I plan to organize these paragraphs in an array and display them one after the other in sequential order upon user interaction. The challenge lies in combining these two aspects through javascript - while the EventListener function works, incorporating the array for display remains unclear. Any guidance on how to achieve this would be highly appreciated.

document.addEventListener("click", (e) => {
  const { clientX, clientY } = e; //get the click position

  //create the div
  const div = document.createElement("div");

  //set its text
  div.innerText = "text";

  //set its position and position style
  div.style.position = "absolute";
  div.style.left = clientX + "px";
  div.style.top = clientY + "px";

  document.body.append(div); //add div to the page
});

Any suggestions on how to integrate the array of paragraphs so that they are displayed one by one with each click?

Answer №1

To achieve this, you will require an Active Tracker Variable

const phrases = [
  "Line: One",
  "Line: Two",
  "Line: Three",
  "Line: Four",
  "Line: Five",
  "Line: Six",
  "Line: Seven",
  "Line: Eight",
  "Line: Nine",
  "Line: Ten"
];

// Keeping Track of the Current Phrase
let currentLineIndex = 0;

document.addEventListener("click", (e) => {
  // Exits once all Phrases are shown
  if (currentLineIndex === phrases.length) return;

  const {
    clientX,
    clientY
  } = e; // Obtaining click position

  // Creating a new div element
  const divElement = document.createElement("div");

  // Assigning text to the div
  divElement.innerText = phrases[currentLineIndex];
  currentLineIndex++;

  // Setting its position and styling
  divElement.style.position = "absolute";
  divElement.style.left = clientX + "px";
  divElement.style.top = clientY + "px";

  document.body.append(divElement); // Adding div to the page
});

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

In my React JS class component, I am looking to have the image display switch each time the button is clicked

How can I change the image when clicking a button in a class component? I am familiar with function components, but unsure how to achieve this. class CoffeeImages extends React.Component{ constructor(props){ super(props) ...

Warning: The use of the outdated folder mapping "./" in the "exports" field for module resolution in the package located at node_modulespostcsspackage.json is deprecated

I recently upgraded my Node to version 16 and since then I have been encountering this issue while building my Angular app. Warning: The folder mapping "./" used in the "exports" field of the package located at ".../node_modules/postcss/package.json" is de ...

JavaScript comparing elements within an array

I need help extracting all the names from an array with URLs containing '/blekinge' and presenting them in a list. Here's what I have so far: const allLocations = locations.map((location) => <li>{location.url}</li> ) I&a ...

When using Vue.js, the class binding feature may not function properly if it is referencing another data property that has variants

I am currently developing a basic TODO application. Within my index.html file, I have a main div with the id #app. Inside this div, there is another div with the class .todobox where I intend to display different tasks stored in my main.js file. Each task ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

Guide to accessing a nested and potentially optional object property with a default value and specifying its data type

Just a simple query here... my goal is to extract data.user.roles, but there's a possibility that data may be empty. In such cases, I want an empty array as the output. Additionally, I need to specify the type of user - which in this instance is any. ...

What improvements does IE7 offer compared to IE6?

Many developers in the web development industry often express frustration when it comes to developing for IE6. But when working with a powerful JavaScript framework such as jQuery, does the process of developing for IE6 differ significantly from that of ...

Including jQuery in an Angular project generated with JHipster

I am facing a challenge with integrating jQuery into my Jhipster Angular project as a newcomer to Jhipster and Angular. My goal is to customize the theme and appearance of the default Jhipster application, so I obtained a theme that uses a combination of ...

JavaScript code that uses jQuery does not function properly on an HTML form

Hello everyone, I am having trouble with some JavaScript code. Here is what I have: $(document).ready(function(){ $(".replyLink").click(function(){ $("#form-to-"+this.id).html(htmlForm()).toggle(500); return false; }); function htmlForm(){ var htm ...

Discovering the nearest sibling using jQuery

My HTML code snippet is as follows: $(".remove-post").click((event) => { $(event.target).fadeOut(); } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="side-bar"> <b ...

Troubleshooting loading specific story types on hacker news API with Angular.js

I have been working on a hacker news client using the official hacker news firebase API and Angular.js. Everything seems to be working fine except for the 'jobstories' posts, which are not rendering on the screen even though the story IDs are bei ...

Invoking a React function repeatedly (every second)

Currently, I am working with React and utilizing Material UI to create a modal. The modal is rendered as part of the body of the code, placed at the bottom of the page. Its visibility is controlled by the state; if it's open or closed. However, I&apos ...

Managing information retrieved from an asynchronous HTTP request

Here is the script I am using to load an HTML page generated by a node.js server: (function nodeLoader(){ $.ajax({ url: "http://oclock.dyndns.org:8000", method: "get", data: {hk: hk }, success: f ...

How can I access the rendered HTML element from a component in Vue 3?

This particular component is known as LayerComponent (placeholder). <script> export default { data() { return { count: 0 } } } </script> <template> <button @click="count++">You have clicked me {{ count ...

Issue with JavaScript causing circles to form around a parent div

I am struggling to position a set of circles around a parent div in my code. I want 6 circles to form a circle around the parent div, but they are not lining up correctly. Can someone help me identify what I'm doing wrong? var div = 360 / 6; var ra ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

Querying MongoDB findAndModify: <<< locate and modify an object within an array in a document

Question : I am attempting to locate an object within a document's array and make updates to it. The syntax below provides me with the desired object, but I am unsure of how to update it. I attempted to use this query in findAndModify, but it seems ...

The combination of useEffect and Client component has the power to greatly

As a newcomer to React development, I've encountered a blocking error in my code that isn't being detected by Visual Studio Code. Here's the code snippet for my NavBar component: import React, { useState, useEffect } from "react"; import Ima ...

When it comes to using the jQuery get method, it may not always yield the desired results. However, utilizing it within a form context,

Upon loading my website, users are directed to a file named index.jsp. My goal is to send a get request to one of my servlets when this page loads. The initial URL is: localhost:8080/Test/ The servlet should perform an action when the URL changes to: loc ...

What are some strategies for improving the efficiency of this function that includes three loops?

In my project, I have developed a function that iterates through various elements in a gantt chart that denote tasks. Each task is identified by the class "link" and attributes "id" and "pre". The attribute "pre" signifies the predecessor task for each sp ...