0

I have three links that express three different images. I want to push these links to an array and I can show off my images on the screen. How can I do that

This is my JS code, is that correct ?? Thanks a lot.

let cardsource = [
    "<a href = 'https://bom.to/Qd87vw'> Card 1</a>",
    "<a href = 'https://bom.to/P21flg'>Card 2 </a>",
    "<a href = 'https://bom.to/dFwwNw'>Card 3 </a>",
];
for (var index = 0; index < cardsource.lengh; index++) {
    console.log(cardsource[index]);
}

2
  • Normally one would only store URL in the array and generate HTML from them when needed. But what you have here would do too. Commented Oct 1, 2021 at 10:35
  • I would look into Document.createElement() for this. Commented Oct 1, 2021 at 10:35

2 Answers 2

1

Don't push tag inside array, just push the link which directly gives you the image.

let cardsource = ['https://bom.to/Qd87vw', 'https://bom.to/P21flg', 'https://bom.to/dFwwNw'];

cardsource.map((item) => <img src={item}>)

This is just to give you the logical view on what you must do instead what you have done. If you are working with .jsx this code would work.

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

4 Comments

If you give a code example, please make sure it's valid. Your example is not valid Javascript.
yes, edited my answer accordingly. Thank you
There is no jsx in the op tags, it's like giving an example of python when question is about javascript...
ok, what do you want me to do?
0

Please use insertAdjacentHTML function. Reference

let cardsource = [
    "<a href = 'https://bom.to/Qd87vw'> Card 1</a>",
    "<a href = 'https://bom.to/P21flg'>Card 2 </a>",
    "<a href = 'https://bom.to/dFwwNw'>Card 3 </a>",
];
cardsource.map(val => document.body.insertAdjacentHTML('beforeend', val));

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.