0

I want to disable a certain group of buttons at a specific time. Using this js:

document.getElementById("reset").setAttribute('disabled','disabled');

But all IDs are different, the buttons is like <button id=1> <button id=2> etc. Can I set some made up variable like <button group="hello">

And then do something like this:

document.getElementById("group:hello").setAttribute('disabled','disabled');

to disable all of the buttons, even though the ID attribute is different from button to button?

2
  • 1
    You can give the elements class names and then use getElementsByClassName() to retrieve them. That returns a list of elements, and you would iterate over the list and set the attribute on each one separately. Commented Mar 19, 2018 at 15:00
  • 2
    With jQuery, that's a little easier. Commented Mar 19, 2018 at 15:00

8 Answers 8

1

what you can do is use the data attribute, see next:

<button  data-group="hello" id="button1">test1</button>
<button  data-group="hello" id="button2">test2</button>
<button  data-group="hello" id="button3">test3</button>

and the JS

 document.querySelectorAll('[data-group="hello"]').forEach(function(button) {
      button.disabled = true;
});

edit, here is the fiddle https://jsfiddle.net/xycmj4gv/2/

Sign up to request clarification or add additional context in comments.

3 Comments

You could chain forEach to querySelectorAll, without creating buttonList variable.
@Kirill I am not sure but i guess forEach have compatibility issue in some browser. I may be wrong.
0

Use class instead of ID. Code is easy:

document.getElementsByClassName("group1").setAttribute('disabled','disabled');

2 Comments

This only gives you the first element
Didn't work. Only one of all buttons disables even though they have the same class
0

Since you tagged jquery

$(".mybutton").attr("disabled", true);

Where mybutton is

<button class= "mybutton"> </button>

Comments

0

Use class instead of id as follows:

var elements = document.getElementsByClassName("{yourClassName}");
var i;
for (i = 0; i < x.length; i++) {
    elements[i].setAttribute(“disabled”, “red");
}

<button class="{yourClassName}”></button>

Comments

0

You can use getElementsByTagName to get all buttons and then You can use Array.Prototype.forEach to disable all buttons like below. forEach will loop through all element and add desired attribute(in your case disabled) to all found elements.

Array.prototype.forEach.call
(document.getElementsByTagName("button"), function(e) {
      e.setAttribute('disabled','disabled');
 });
<button id="1">12</button> <button id="2">12</button>
<button id="3">12</button> <button id="4">12</button>

Comments

0

You can use wildcards for Id's:

Starts-with selector

document.querySelector("[id^=myId]")

works for

id="myId1"
id="myId2"
...

or Ends-with selector

document.querySelector("[id$=myId]")

works for

id="firstMyId"
id="secondMyId",...

Hint: Please don't start Id's with numbers like: id="1myId". That's not w3c conform

Comments

0

use class to group buttons, like this:

var btnsGroup = document.getElementsByClassName('group');
Array.prototype.forEach.call(btnsGroup, function(button) {
  button.setAttribute('disabled','disabled');
});

Comments

0

You can do something like this-

var buttons = document.querySelectorAll("[reset='true']");

for (i = 0; i < buttons.length; ++i) 
{
    buttons[i].setAttribute('disabled','disabled');
}

And HTML goes like this-

<button reset="true" >1 Candy</button>
<button reset="true" >2 Candy</button>

2 Comments

querySelectorAll has same browser support as forEach, so you might as well use it instead of for loop. Otherwise worth using one of the older DOM navigation functions for better browser support. caniuse.com/#feat=queryselector caniuse.com/#feat=es5
@Kirill Yeah, I am doing it like this because of browser support.

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.