4

Can anybody let me know if there is anything wrong with the following code. I'm trying to get the value of a input type="file" form tag but the alert keeps telling me it's 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" />

Thanks in advance

James

2
  • Sorry to bother you guys. It turns out I gave my form the same ID as the input field. Cheers Commented May 19, 2011 at 16:18
  • Either post an answer yourself or press delete link below the question. Commented May 19, 2011 at 16:19

5 Answers 5

2

Works for me.

See working jsFiddle demo:

$('#submitForm').bind("click",function() { 

    var imgVal = $('#imageUpload').val(); 
    alert(imgVal);             
    return false;

});
Sign up to request clarification or add additional context in comments.

Comments

2

This should work on any browser

$('#imageUpload').attr('value')

Comments

2

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

One thing to remember is that many browsers will give you the file, but it will hide the actual path with something like this: C:/fakepath/filename.extension

Comments

0

when any user select file then to detect in jquery is file element value change or not put this code in 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
		
		
	});
	
});

file type button is enable and upload button of form is disable when user will select file then it's value will change and enable upload button which means then we can submit our form.

Comments

-2

You can use

$('#imageUpload').value

1 Comment

Incorrect, this outputs undefined. Since you're querying the element with jQuery, you'd want to use .val() instead of .value. If you really wanted to use jQuery (for instance selecting the element and doing other things via chaining before getting the value), you could end with [0].value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.