Skip to content
Advertisement

How can I create an img element when pressing a button with JavaScript?

When I click I want an img tag to be generated and with an image I have tried several ways but it doesn’t work.

JavaScript
JavaScript

Advertisement

Answer

Just set the image’s .src property to the source URL.

create is an ID, not a class, so use document.getElementById().

The anchors don’t have class="a", you should use document.getElementsByClassName("nav-link").

You need to use Event.preventDefault() in the event listener to prevent following the links.

You can’t use new as a variable name because it’s a reserved keyword.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement