1

I am creating an interactive website which consists of multiple paragraphs gradually being displayed once the user clicks anywhere on screen. I want these paragraphs put into an array and then displayed in the order that they are set. When a user clicks screen - show paragraph 1, when a user clicks screen again - show paragraph 2 and so on while keeping them all on the screen.

However I am not sure how to combine both these elements in javascript, I have the EventListener function working but how would I go about displaying the array. In a for loop etc? If anyone knows how to do this it would be greatly 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
});

How would I incorporate the array paragraphs on each click one by one?

5
  • When the user clicks a second time, do you want the paragraph 2 to be displayed separately or in place of Para 1? Commented Nov 24, 2022 at 14:24
  • Placed separately as the user is able to click anywhere on the screen and the text appears in that exact mouse location Commented Nov 24, 2022 at 14:25
  • isn't already what you did in your listener ? Commented Nov 24, 2022 at 14:26
  • That's a placeholder for now, I want to place the array where it says "text" and create a for loop as with each the click on the screen the paragraph is different Commented Nov 24, 2022 at 14:28
  • Create an array of strings, each element holding the paragraph content. Then in your click handler increment the index, and set div.innerText to the array element at that index. Commented Nov 24, 2022 at 14:30

1 Answer 1

2

You just need an Active Tracker Variable

const texts = [
  "Paragraph: 1",
  "Paragraph: 2",
  "Paragraph: 3",
  "Paragraph: 4",
  "Paragraph: 5",
  "Paragraph: 6",
  "Paragraph: 7",
  "Paragraph: 8",
  "Paragraph: 9",
  "Paragraph: 10"
];

// Keeps Track of Current Text
let currentParaIdx = 0;

document.addEventListener("click", (e) => {
  // Stop once All Texts are Displayed
  if (currentParaIdx === texts.length) return;

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

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

  //set its text
  div.innerText = texts[currentParaIdx];
  currentParaIdx++;

  //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
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.