Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : get all the object where id is like log_XXXX

Tags:

javascript

I need to get all the objects whose id matches a specific pattern . How can I do it? Thanks!

like image 483
Yossale Avatar asked Apr 23 '09 20:04

Yossale


1 Answers

Current Browsers:

// DOM collection as proper array
const matches = Array.from(document.querySelectorAll('[id^=log_]'));

Older Browsers: (IE9+)

// Use Array.prototype.slice to turn the DOM collection into a proper array
var matches = [].slice.call(document.querySelectorAll('[id^=log_]'));

jQuery:

$('[id^=log_]')

Really Old Browsers, no jQuery:

var matches = [];
var elems = document.getElementsByTagName("*");
for (var i=0; i<elems.length; i++) {
  if (elems[i].id.indexOf("log_") == 0)
    matches.push(elems[i]);
}
//matches now is an array of all matching elements.
like image 196
Tracker1 Avatar answered Oct 24 '22 01:10

Tracker1