LoginSignup
1
0

More than 3 years have passed since last update.

getElementByIdとqueryselectorの表記の違い

Posted at

こんにちは。

今日はJavascriptのDOMで使用するgetElementByIdとqueryselectorの表記の違いについて備忘録として書いておこうと思います。

getElementByIdは('')内に何もいらないが、querySelector('')はidなら#、classなら.が必要

例えば以下のような変数の宣言であれば、問題なく書くことが出来ますが、

const heading =document.getElementById('heading');
const text =document.getElementById('text');

以下のような形ではエラーになります。

const hide = document.querySelector('switch');
const familyList =document.querySelector('famiy');

querySelector()には必ず()内に「#」が「.」が必要なためですね。

簡単なことではあるのですが、初心者のため知らなかったので今回知れて良かったです。

1
0
0

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
0