0

I want to add hyperlink (a href tag ) to img.src=image_url +Trending_movie.poster_path

function Trending_movies_section(trending_movies) {
  const section = document.createElement('section');
  section.classList = 'section';
  trending_movies.map((Tending_movie) => {
    if (Tending_movie.poster_path) {
      const img = document.createElement('img');
      img.setAttribute('class', 'trending_images');
      a.href = "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_img_src2";
      img.src = image_url + Tending_movie.poster_path;
      section.appendChild(img);
    }
  })
  return section
}
3
  • I think you're supposed to show the HTML instead of Javascript? Or I really get it wrong. Commented Sep 21, 2020 at 5:59
  • You need to wrap your img tag with the a tag in html and where your a is declared? Commented Sep 21, 2020 at 6:00
  • 1
    Can you please describe the expected and actual behavior? Commented Sep 21, 2020 at 6:04

1 Answer 1

1

Just wrap in an anchor

No need to use map when you do not need the resulting array

function Trending_movies_section(trending_movies) {
  const section = document.createElement('section');
  section.classList = 'section';
  trending_movies.forEach(movie => {
    if (movie.poster_path) {
      const img = document.createElement('img');
      const a = document.createElement('a');
      img.setAttribute('class', 'trending_images');
      a.href = "...";
      img.src = image_url + movie.poster_path;
      a.append(img)
      section.appendChild(a);
    }
  })
  return section;
}
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.