13

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

2
  • 2
    Be more precise. "bib" can be any character? Do you mean * can be any character? so that bibo matches but bibliography doesn't? Commented Jun 15, 2009 at 23:06
  • Can you use CSS style attribute selectors? I imagine document.getElementsByTagName('[id^=bib]'); would work. Commented Oct 18, 2012 at 16:43

3 Answers 3

17

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of cycling through them all every time... cache the elements in an array so the next cycle will be quicker.
TJ, that would be good if the code is running more than once, and there are a relatively small number of matching elements.
2
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;
        }
    }
}

Comments

0

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

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

1 Comment

for me this always return an HTMLElement collection with no elements, despite having lots of elements starting with "bib" (or whatever I replaced those characters with)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.