JQuery getting field value input type = "file"

Can someone tell me if there is something wrong in the following code. I am trying to get the value of the form tag input type = "file", but the warning keeps telling me that it is empty

$('#submitForm').bind("click",function(){ var imgVal = $('#imageUpload').val(); alert(imgVal); return false; }); <input name="imageUpload" id="imageUpload" type="file" value="" class="text"/> <input name="submit" type="submit" id="submitForm" value="Submit Photo" /> 

Thank you in advance

James

+4
source share
5 answers

It works for me.

See a working jsFiddle demo :

 $('#submitForm').bind("click",function() { var imgVal = $('#imageUpload').val(); alert(imgVal); return false; }); 
+2
source

This should work in any browser.

 $('#imageUpload').attr('value') 
+2
source

You can also do: var imgVal = document.getElementById("imageUpload"); Then, to get the value, do something like this: alert(imgVal.value);

Remember that many browsers will provide you with a file, but it will hide the actual path with something like this: C:/fakepath/filename.extension

+1
source

when any user selects a file and then to detect in jquery the value of the file element changes or not put this code in the jQuery file

 $(document).ready(function() { //$("#upload").attr("disabled","true"); $("#upload").attr("disabled","disabled"); $("#file").change(function() { //alert("someting change"); //value = $(this).val();//get fake path of file //alert(value); $("#upload").removeAttr("disabled"); //enabling upload button }); }); 

the file type button is turned on, and the form’s download button is disabled when the user selects the file, then the value will change and the download button will turn on, which means that we can send our form.

0
source

you can use

 $('#imageUpload').value 
-2
source

All Articles