DEV Community

Austin
Austin

Posted on

how do i add pictures in javascript

i have been having a hard time trying to get pictures into my script i have been using JSFiddle and i need help

Top comments (6)

Collapse
 
prakhart111 profile image
Prakhar Tandon

You can use document.createElement() method to create an an empty img element, then add the source.

function img_add() {
            var img = document.createElement('img');
            img.src = #    //link to image
            document.getElementById('body').appendChild(img);
}
Enter fullscreen mode Exit fullscreen mode

I think that's what you were looking for, right?

Collapse
 
azloop profile image
Austin

yes thank you

Collapse
 
curiousdev profile image
CuriousDev

If you want to load an image into the page, this looks fine. If you do not want to load it into the DOM, then you should be able to do a simple HTTP request to get the image.
Can you please describe the issue more, @looph8le ?

Collapse
 
azloop profile image
Austin

i dont know how to add pictures to javascript

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Get pictures from where? If it's from an API then fetch is your friend. If it's from your computer you can use an <input type="file" /> + an eventlistener; onChange should work.

BTW I would suggest to change the #tutorial tag for #help

Collapse
 
theawesomeguy47 profile image
TheAwesomeGuy47

don't know but use #help as a tag in the future