Find all elements whose id begins with a common string

JavascriptRegex

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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]);

Solution 2 - Javascript

You should have just used simple CSS selector together with JavaScript's .querySelectorAll() method.

In your case :

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

Solution 3 - Javascript

Because you didn't tag jQuery, and you probably don't need it, my suggestion would be to add a class to these elements when you create them. Then use the getElementsByClassName() function that's built into most browsers. For IE you would need to add something like this:

if (typeof document.getElementsByClassName!='function') {
    document.getElementsByClassName = function() {
        var elms = document.getElementsByTagName('*');
        var ei = new Array();
        for (i=0;i<elms.length;i++) {
            if (elms[i].getAttribute('class')) {
                ecl = elms[i].getAttribute('class').split(' ');
                for (j=0;j<ecl.length;j++) {
                    if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                        ei.push(elms[i]);
                    }
                }
            } else if (elms[i].className) {
                ecl = elms[i].className.split(' ');
                for (j=0;j<ecl.length;j++) {
                    if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                        ei.push(elms[i]);
                    }
                }
            }
        }
        return ei;
    }
}

Solution 4 - Javascript

function idsLike(str){
	var nodes= document.body.getElementsByTagName('*'),
	L= nodes.length, A= [], temp;
	while(L){
		temp= nodes[--L].id || '';
		if(temp.indexOf(str)== 0) A.push(temp);
	}
	return A;
}

idsLike('createdOn')

Solution 5 - Javascript

Try the following:

var values = new Array(valueKey_1);
var keys = new Array("nameKey_1");
var form = document.forms[0];
for (var i = 0; i < form.length; i++) {
    name = form.elements[i].name;
    var startName = name.toLowerCase().substring(0, 18);
    if (startName == 'startStringExample') {
	values.push(name.value);
	keys.push(name);
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmerileyView Question on Stackoverflow
Solution 1 - JavascriptPhrogzView Answer on Stackoverflow
Solution 2 - JavascriptDautView Answer on Stackoverflow
Solution 3 - JavascriptIsaac FifeView Answer on Stackoverflow
Solution 4 - JavascriptkennebecView Answer on Stackoverflow
Solution 5 - JavascriptPeterPinkView Answer on Stackoverflow