Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  30] [ 5]  / answers: 1 / hits: 19355  / 15 Years ago, thu, april 23, 2009, 12:00:00

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


More From » javascript

 Answers
11

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.

[#99659] Friday, April 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrencegreysons

Total Points: 674
Total Questions: 102
Total Answers: 105

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;