Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what listeners are called when a file is selected from a file chooser in javascript

I'm working on creating an upload button for my website but I'm unable to trigger events based on when the file is selected for the upload. I was under the impression that the .submit function did this but I'm unable to get any results from this. HTML

    <input type="button" id="uploadbutton" value="Upload File" />
    <form action="includes/uploader.php" method="POST" id="fileupload_form">
        <input id="fileupload" type="file" name="files[]" multiple style="display:none;">
        <input id="server" type="hidden" name="server" value="upload">
        <input type="hidden" id="storeA" value="statement_upload" name="storeA">
    </form>

Javascript/jQuery

var fileupload = $("#fileupload");

$("#uploadbutton").click( function(){
    fileupload.click();
});

$("#fileupload_form").submit( function(e){}

This javascript function will not run when trying to select the file and no errors are given. Any and all help is appreciated!

like image 212
DitR Avatar asked May 22 '13 20:05

DitR


People also ask

What are listeners in JavaScript?

An event listener in JavaScript is a way that you can wait for user interaction like a click or keypress and then run some code whenever that action happens. One common use case for event listeners is listening for click events on a button. const button = document.

What is action listener in JavaScript?

The JavaScript addEventListener() method allows you to set up functions to be called when a specified event happens, such as when a user clicks a button. This tutorial shows you how you can implement addEventListener() in your code.

What attribute is used to input data into a file?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.

How do you check if file is uploaded or not in JavaScript?

length property to check file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.


1 Answers

Listen on fileupload.on('change', function(){}) to get what and when was chosen in file selector.

like image 86
Grzegorz Kaczan Avatar answered Oct 20 '22 03:10

Grzegorz Kaczan