LoginSignup
1
1

More than 3 years have passed since last update.

DOM上のすべてのid属性を取得する

Last updated at Posted at 2021-03-31

※filterメソッド使わなくてもcssセレクターで可能とのこと。詳しくはコメント欄を参照。

id属性以外でも可。

//id属性が指定されているエレメントを取得して配列化する
var elm = Array.prototype.slice.call(document.querySelectorAll("*"))
  .filter(d => d.id);

//id名を取得する
var idList = elm.map(d => d.id);

正規表現を使って取得するエレメントを選択する。
例えばid名が"btn_"から始まるエレメントすべてを取得する

var elm = Array.prototype.slice.call(document.querySelectorAll("*"))
  .filter(d=> /^btn_/.test(d.id));

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