0
0

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.

Array.from

Posted at

###HTMLCollectionで配列系メソッドは使えないらしい

最近JSに触れる機会があるので、備忘録がてら。

const fields = document.getElementsByClassName(
    'elements'
  );

上記のように、getElementsByClassNameで取得できるHTMLCollectionは、配列系のメソッドを利用できないらしい。

そのため、Array fromを使って配列に変換
elements = Array.from(elements)

###サンプル
配列系のメソッド今回の場合はforEachを使った実例。

const fields = document.getElementsByClassName(
    'elements'
  );
Array.from(elements).forEach((element) => {
    const teikies = field.getElementsByClassName(
      '
    );
    Array.from(teikies).forEach((teiki) => {
      link.addEventListener('click', () => {
        const inputs = field.getElementsByTagName('input');
        Array.from(inputs).forEach((input) => {
          const type = input.getAttribute('type');
          const masked = 'icoca';
          const unmasked = 'suica';
          if (type === 'password') {
            input.setAttribute('type', 'text');
            field.classList.remove(icoca);
            field.classList.add(suica);
          } else {
            input.setAttribute('type', 'password');
            field.classList.remove(suica);
            field.classList.add(icoca);
          }
        });
      });
    });
  });
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?