Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  151] [ 7]  / answers: 1 / hits: 163121  / 11 Years ago, thu, august 1, 2013, 12:00:00

I am triggering a file upload on href click.

I am trying to block all extension except doc, docx and pdf.

I am not getting the correct alert value.



<div class=cv> Would you like to attach you CV? <a href= id=resume_link>Click here</a></div>
<input type=file id=resume style=visibility: hidden>


Javascript:



        var myfile=;
$('#resume_link').click(function() {
$('#resume').trigger('click');
myfile=$('#resume').val();
var ext = myfile.split('.').pop();
//var extension = myfile.substr( (myfile.lastIndexOf('.') +1) );

if(ext==pdf || ext==docx || ext==doc){
alert(ext);
}
else{
alert(ext);
}
})


MyFiddle..its showing error


More From » jquery

 Answers
7

Better to use change event on input field.



Updated source:



var myfile=;

$('#resume_link').click(function( e ) {
e.preventDefault();
$('#resume').trigger('click');
});

$('#resume').on( 'change', function() {
myfile= $( this ).val();
var ext = myfile.split('.').pop();
if(ext==pdf || ext==docx || ext==doc){
alert(ext);
} else{
alert(ext);
}
});


Updated jsFiddle.


[#76594] Wednesday, July 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;