formタグ内でのみjavascriptのイベントが発生しません。
想定している動きとしては、
アップロードボタンの押下を行うとコンソールに「hello」を表示させます。
下記のコードだとエラーが出ます。
html
<form> <button id="button" class="button">アップロード</button> <input style="display: none;" class="input" id="input" type="file"> </form>
javascript
const button = document.getElementById('button'); const input = document.getElementById('input'); button.addEventListener("click", () => { input.click(); }); input.addEventListener('change', (event) => { console.log('hello'); }); });
しかしbuttonとinputをformタグから出すとはエラーが出ません。
正常にコンソールへ「hello」が出力されます。
html
<button id="button" class="button">アップロード</button> <input class="input" id="input" type="file">
なにが起きているのかわからないので対策が打てなくて困っています。
お力をお借りできればと思います。
よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう