LoginSignup
0
0

More than 3 years have passed since last update.

【JavaScript】getElementsByTagNameの結果を配列にして配列のメソッドを使う方法

Last updated at Posted at 2020-11-14

getElementsByTagNameした結果をfindしたかったのですが、そのままでは使えないんですね。
HTMLCollectionオブジェクトが返ってきました。

sliceを使って、配列にしてみました。

const tags = document.getElementsByTagName('script');
const targetTag = Array.prototype.slice.call(tags).find(c => c.src?.match(/test.com/)));

もしくは、Array.fromで、

const tags = document.getElementsByTagName('script');
const targetTag = Array.from(tags).find(c => c.src?.match(/test.com/)));

もしくは、スプレッド演算で、

const tags = document.getElementsByTagName('script');
const targetTag = [...tags].find(c => c.src?.match(/test.com/)));
0
0
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
0
0