How can I set the value of an input from a file type in a javaScript variable? this is my input:
<input type="file">
How can I set the value of an input from a file type in a javaScript variable? this is my input:
<input type="file">
You can not set the value of a file input with Javascript.
The only way to set the value of a file input is by the user to select a file.
This is done for security reasons. Otherwise you would be able to create a Javascript that automatically uploads a specific file from the clients computer.
To get the value of a file input
, use document.getElementById("fileinputid").value
. However, it will give you the value mangled with something like c:\fakepath\
to keep the details of the user's filesystem private.
Setting the Value of the Input Element using JavaScript Variable
document.addEventListener("DOMContentLoaded", function(event) {
var input = document.getElementsByTagName('input');
input[0].setAttribute("value", "c:\\yourPath\\");
//The extra backslashes are to ignore each backslash
});
<input type="file">