With the web application am developing student discussion platform, I want a student to attach file when posting a question, I do not want to use a button, Instead, a student just clicks a hyperlink and the upload dialog box opens.
How to do it?
With the web application am developing student discussion platform, I want a student to attach file when posting a question, I do not want to use a button, Instead, a student just clicks a hyperlink and the upload dialog box opens.
How to do it?
You can create file input inside the link and hide it with opacity. Also to make it accessible you can write a little bit of JavaScript.
link.addEventListener('keydown', (e) => {
const { key } = e;
if (key === 'Enter') {
e.preventDefault();
file.click();
}
});
a {
position: relative;
}
#file {
position: absolute;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
-moz-opacity: 0;
-khtml-opacity: 0;
opacity: 0;
cursor: pointer;
right: 0;
}
<a href="#" id="link"><input type="file" id="file"/>Link text</a>