Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  127] [ 6]  / answers: 1 / hits: 100936  / 12 Years ago, wed, april 11, 2012, 12:00:00

I have a XSL that created multiple elements with the id of createdOn plus a $unique-id



Example : createdOnid0xfff5db30


I want to find and store these in a variable using JavaScript. I've tried



var dates = document.getElementsById(/createdOn/);


but that doesn't appear to work.


More From » regex

 Answers
7

Using jQuery you can use the attr starts with selector:


var dates = $('[id^="createdOnid"]');

Using modern browsers, you can use the CSS3 attribute value begins with selector along with querySelectorAll:


var dates = document.querySelectorAll('[id^="createdOnID"]');

But for a fallback for old browsers (and without jQuery) you'll need:


var dateRE = /^createdOnid/;
var dates=[],els=document.getElementsByTagName('*');
for (var i=els.length;i--;) if (dateRE.test(els[i].id)) dates.push(els[i]);

[#86305] Tuesday, April 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jillcalistay

Total Points: 561
Total Questions: 94
Total Answers: 114

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
;