2👍
✅
Problems:
You put your input
inside the label
tag. I learned this is okay.
- Why using a
button
outside the wholelabel
andinput
tags? I don’t understand what your intention was with that. - You replace the inside of the
label
tag by a text node (containing the file name). Through your replacement you delete yourinput
field out of the DOM. SeequerySelector
line in your code. - After having replaced the input field, there is no
fileName
on the page anymore, so itgetElementById
results innull
.
Solution:
- (optionally) I’d delete the
button
tag and only keep the inside of it. - Don’t operate on that
label
and don’t try to overwrite the value of a fileinput
field - By not removing the
input
field anymore, you can access it and read out the file name
See my example here: https://stackblitz.com/edit/js-fdxxnf
1👍
You can try with ref
:
new Vue({
el: '#demo',
data() {
return {
projectFile: true
}
},
methods: {
changeLbl() {
this.$refs.lab.innerText = 'filename'
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div class="row">
<div class="clg6 cmd8 csm10 cxs12" v-if="projectFile">
<button
class="button primary outline medium upload">
<label ref="lab">
<input
type="file"
name="projectFile"
id="fileName"
accept=".xls, .xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
v-on:change="changeLbl"/>
<i class="fas fa-cloud-upload"></i>
Upload Project File
</label>
</button>
</div>
</div>
</div>
Source:stackexchange.com