I am trying to upload files.The thing is that When the user click submit button before choosing the file,user should get any alert.If files is already selected,the value of submit
button should change to Updating...
.For this I have used the change()
method,but it is not working properly.
javascript
file:
$(document).ready(function()
{
var file_name='';
//for checking whether the file queue contain files or not
$('#upload').click(function()
{
$('#file').live('change',function(){
for (var i = 0; i < this.files.length; i++)
{
file_name = this.files[i].name;
//alert(file_name);
if(file_name !== null && file_name !== undefined)
{
$('#upload').attr('value','Updating..');
}
else
{
alert('error');
}
}//end for loop
});
});
});
html:
<form id='file-form' action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" ><br>
<input type="button" name="upload" id='upload' value="Upload file">
</form>
please help me.
As per JQuery documentation .live is deprecated use on method.
$('#file').on('change',function(){
//your code
});
try this one
You initialised file_name to ''. Yet in your comparison you are checking whether it is equal to null or undefined, neither of which would be true when unset.
Instead of:
if(file_name !== null && file_name !== undefined)
you are probably looking for:
if (file_name !== '')