0

Value is not working for type="file"

<html>
  <body>
  <input type="file" id="myText">
  <script>
  document.getElementById("myText").value = "Johnny Bravo";
  </script>
  </body>
</html>
Ali Esmailpor
  • 1,209
  • 3
  • 11
  • 22
  • 4
    You can't fill the value of `input type="file"` with JS. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file . Also, `file` seems to be incorrect type for the use-case, use `text` instead. – Teemu Mar 20 '21 at 08:37

2 Answers2

1

Input Type file cannot be filled with any value. The choosen file will be shown only on selection of the file. Perhaps, you could get the value and show as preview image/video.

SajZ
  • 262
  • 3
  • 16
1

For security reason, the input[type='file'] is not changeable via JavaScript.

For example, if you can change the value of input[type='file'] via JavaScript, you can read any file content from client's local machine, it is potential risk, right?

Steve Gao
  • 585
  • 2
  • 5
  • 24