<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
title
>
How to detect when cancel
is clicked on file input?
</
title
>
</
head
>
<
body
>
<
input
type
=
'file'
id
=
'theFile'
onclick
=
"initialize()"
/>
<
script
>
// Get the file input element
var theFile =
document.getElementById('theFile');
// Define a function to be called
// when the input is focused
function initialize() {
document.body.onfocus = checkIt;
console.log('initializing');
}
// Define a function to check if
// the user failed to upload file
function checkIt() {
// Check if the number of files
// is not zero
if (theFile.value.length) {
alert('Files Loaded');
}
// Alert the user if the number
// of file is zero
else {
alert('Cancel clicked');
}
document.body.onfocus = null;
console.log('checked');
}
</
script
>
</
body
>
</
html
>