meriley
meriley

Reputation: 1881

Find all elements whose id begins with a common string

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.

Upvotes: 86

Views: 120719

Answers (5)

Daut
Daut

Reputation: 2645

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

In your case :

var idStartsWith = "start"

var items = document.querySelectorAll(`[id^=${idStartsWith}]`);

if you want to get the end just change ^ for $

var idEndsWith = "end"

var items = document.querySelectorAll(`[id$=${idEndsWith}]`);

Upvotes: 25

Phrogz
Phrogz

Reputation: 303421

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

Upvotes: 155

PeterPink
PeterPink

Reputation: 109

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

Upvotes: 1

kennebec
kennebec

Reputation: 104820

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')

Upvotes: 3

Isaac Fife
Isaac Fife

Reputation: 1690

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;
    }
}

Upvotes: 7

Related Questions