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