0

I'm passing an array of strings to be displayed as html text inside a

my_arr = ["PRODUCT", "SHAMPOO1", "SHAMPOO2", "SHAMPOO3", "SHAMPOO1"]

which is displayed as :

PRODUCT, SHAMPOO1, SHAMPOO2, SHAMPOO3, SHAMPOO1.

What I want to do is make these clickable. And when I click on one of these words I want that word to be displayed below it.

What's the cleanest way to do this? Passing the array in the <a> tag isn't working for me.

3
  • Can you show the code you have used with a tag Commented Aug 4, 2020 at 7:48
  • Just passed the array like this : document.getElementById("products").innerHTML + '<a>' + my_arr + '</a>' Commented Aug 4, 2020 at 7:55
  • Why you want display that word, when you click that word not hover on that word? You may looking for tooltip or title. Commented Aug 4, 2020 at 8:52

1 Answer 1

3

you can add it to the DOM, then loop your array to the li under ul

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <div id="products"></div>
  <body>
    <script>
      var my_arr = ["PRODUCT", "SHAMPOO1", "SHAMPOO2", "SHAMPOO3", "SHAMPOO1"];
      var str = "<ul>";

      my_arr.forEach(function (product) {
        str += "<li> <a href='/'>" + product + "</a> </li>";
      });

      str += "</ul>";
      document.getElementById("products").innerHTML = str;
    </script>
  </body>
</html>

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.