1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptでnull判定をしないといけないよーって話。

Posted at

JavaScriptって、クリック判定したい時にDOM要素とってきて、そいつがクリックされた時〜
って処理を書くけど、そのDOM要素がない時は普通にエラーを吐く。

なので、きちんとnull判定をしておかないといけないよって話でした。

Uncaught TypeError: Cannot read property 'addEventListener' of null

上記エラーが出ないようにするために

var btn = document.getElementById('form_submit_button');

if (btn != null) {
    btn.addEventListener('click', function () {
    document.getElementById('quill_content').value = JSON.stringify(quill_1.getContents());

    //submit()でフォームの内容を送信
    document.quill_form.submit();
});
}

こんな感じでnull判定をする。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?