Hi,
I have a input that looks like this :
<input type="file" id="ModelViewAd.Files[0]" name="ModelViewAd.Files[0]" />
The id and name is set in this way to be able to bind to the viewclass (ASP.NET MVC2).
When I try to run the following jquery command to get value :
$('#ModelViewAd.Files[0]').val()
I get undefined?
What am I doing wrong?
BestRegards
Solution : This is solved by changing the name (HTML4 do not suport [ and ]). But If you are using HTML5 then this can be solved by using escape chars like this : $(‘#ModelViewAd.Files\[0\]’).val().
You need to escape the period character (
.) in the selector. Otherwise, jQuery interprets your selector as looking for an element with IDModelViewAdand classFiles[0]:Once you have actually selected an element (which, I assume, your selector was not properly doing), the
.val()method may still returnundefinedif the user has not actually selected a file.