I want to insert an image using prompt()
in JavaScript
Here is the thing I tried :
var name = prompt("Enter your name");
[Typeof string or number]
What I want is:
var profile_img = prompt("Choose Your Picture");
[Typeof img]
I want to insert an image using prompt()
in JavaScript
Here is the thing I tried :
var name = prompt("Enter your name");
[Typeof string or number]
What I want is:
var profile_img = prompt("Choose Your Picture");
[Typeof img]
No, it is not possible. A window.prompt()
call only accepts text input.
Depending on your use case, the user could enter the public URL to an image (any supported protocol, even the pseude-scheme data:
) and your script could then fetch the image from the given URL. Another possibility mentioned by Andrew in the comments: have users input an encoded version of the image (base64, hexdump, …) and then parse it yourself to get back the binary image data.
To then insert the image into your DOM, see other questions and answers on Stackoverflow, such as DOM appendChild to insert images:
let img = document.createElement("img");
img.src = 'path/to/image';
parent_element.appendChild(img);
What you can do is to use <input type="file" ..
and trigger it with the js
Assuming you have jQuery, but the principle should work regardless
<input id="myInput" type="file" style="visibility:hidden" />
<input type="button" value="Show Dialog" onclick="$('#myInput').click();" />
Then look at this answer how to display the image without uploading it to the server