Diego
Diego

Reputation: 17140

Getting elements by a partial id string in javascript

I have the followng code:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = bDisabled;
}

I need to now add some logic to only disable the the inputs that have and Id of the form "bib*" where bib can be any character. Ive seen other questions where this is done with jquery but I cant use jquery just simple javascript. Any help would be appreciated.

Thanks

Upvotes: 13

Views: 23183

Answers (3)

Eytan
Eytan

Reputation: 1835

I haven't tried this myself, but would CSS style attribute selectors?

document.getElementsByTagName('[id^=bib]');

Upvotes: 0

Ravi
Ravi

Reputation: 439

function CheckDynamicValue(partialid, value) {

    var re = new RegExp(partialid, 'g');
    var elems = document.getElementsByTagName('*'), i = 0, el;
    while (el = elems[i++]) {
        if (el.id.match(re)) {
            el.disabled = value;
        }
    }
}

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

This is pretty basic stuff.

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
  if(inputs[i].id.indexOf("bib") == 0)
    inputs[i].disabled = bDisabled;
}

Upvotes: 17

Related Questions