Reputation: 93
I have an upload function wherein I need to customize the fileinput element in html.. I will need to hide it and replace it with my own button and text inputfield..
my question is, how can I put the filename chosen into my custom text inputfield as a substitute to the inputfield that I've hidden?
here's an initial code:
<input type="file" accept="image/*" name="sandiganFile" id="sandigan" style="display:none"/>
<input type="text" id="sandiganFilename" />
<button type="submit" id="sandiganBrowse" onclick="$('#sandigan').click()">Browse</button>
<button type="button" id="sandiganUpload" name="sandigansubmit" >Upload</button>
Upvotes: 0
Views: 174
Reputation: 1
Try utilizing .change()
, name
property of File
object
$("#sandigan").change(function(e) {
$("#sandiganFilename").val(this.files[0].name);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<input type="file" accept="image/*" name="sandiganFile" id="sandigan" style="display:none"/>
<input type="text" id="sandiganFilename" />
<button type="submit" id="sandiganBrowse" onclick="$('#sandigan').click()">Browse</button>
<button type="button" id="sandiganUpload" name="sandigansubmit" >Upload</button>
Upvotes: 1
Reputation: 16440
as mentioned in comments, you cannot get full path, but doable for filename only.
jQuery(function($) {
$("#sandigan").on('change', function() {
var file = $(this).val();
$("#sandiganFilename").val(file.split('\\').reverse()[0]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" accept="image/*" name="sandiganFile" id="sandigan" style="display:none" />
<input type="text" id="sandiganFilename" />
<button type="submit" id="sandiganBrowse" onclick="$('#sandigan').click()">Browse</button>
<button type="button" id="sandiganUpload" name="sandigansubmit">Upload</button>
Upvotes: 0