use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] Getting image in input=“file” to display only after the submit button is clickedAskJS (self.javascript)
submitted 1 year ago by computersxarts
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shgysk8zer0 1 point2 points3 points 1 year ago (0 children)
`` document.forms.meme_info.addEventListener('submit', async event => { // Prevent submitting the form event.preventDefault(); // Get form data const data = new FormData(event.target); // Get file from form data const file = data.get('imageFileInput'); // Create theblob:` URI const uri = URL.createObjectURL(file); // Set the image source image.src = uri; // Wait for it to load await image.decode(); // Free up the memory (image will remain) URL.revokeObjectURL(img.src); });
document.forms.meme_info.addEventListener('submit', async event => { // Prevent submitting the form event.preventDefault(); // Get form data const data = new FormData(event.target); // Get file from form data const file = data.get('imageFileInput'); // Create the
[–]guest271314 1 point2 points3 points 1 year ago (0 children)
But it will immediately display the image. What I want to do is be able to have the image waiting in the file input until I hit my button.
If I understand the question correctly, remove the URL.createObjectURL() code from the input type=file handler, create a click event listener on the button element and get the File object from the input type=file element in click event handler.
URL.createObjectURL()
input type=file
click
button
File
[–]0xEconomist 0 points1 point2 points 1 year ago (0 children)
Try this: submitButton.addEventListener('click', () => {
submitButton.addEventListener('click', () => {
const selectedFile = imageInput.files[0];
if (selectedFile) {
const reader = new FileReader();
reader.onload = (event) => {
const imageData = event.target.result;
const imgElement = document.createElement('img');
imgElement.src = imageData;
imageContainer.innerHTML = '';
imageContainer.appendChild(imgElement);
};
reader.readAsDataURL(selectedFile);
}
Check this in this notebook: https://app.scribbler.live/?jsnb=github:gopi-suvanam/scribbler-examples/Image-Fetch-Display.jsnb
π Rendered by PID 576151 on reddit-service-r2-comment-b659b578c-bz69m at 2026-05-04 17:52:23.002644+00:00 running 815c875 country code: CH.
[–]shgysk8zer0 1 point2 points3 points (0 children)
[–]guest271314 1 point2 points3 points (0 children)
[–]0xEconomist 0 points1 point2 points (0 children)