-1

this is what I tried So I am trying to disable the options buttons in my quiz app after the user selects a option.

I am doing this because when the correct option is shown after the user selects any option if I click the correct answer again the score counter increases and also if the user selects the wrong answer and then if he selects the correct answer then he can continue the game.

This is the quiz game check it out and see the prob=> https://coderjm1.github.io/guessthisflag.github.io/

HTML

<div id="answer-buttons" class="btn-grid">
    <button  class="btn" onclick="disable()">Answer1</button>
    <button  class="btn" onclick="disable()">Answer2</button>
    <button class="btn" onclick="disable()">Answer3</button>
    <button class="btn" onclick="disable()">Answer4</button>
</div>

JAVASCRIPT

function disable() {
    let elems = document.getElementsByClassName('btn')
    for(let i = 0; i < elems.length; i++){
        elems[i].disable = true;
    }
}
Klaassiek
  • 2,795
  • 1
  • 23
  • 41
  • The attribute is `disabled` rather than `disable`. So, `elems[i].disabled = true;` this would disable the button – Nithish Jul 17 '21 at 07:11

4 Answers4

0

You can just loop through them all with querySelectorAll and setAttribute('disabled',true)

function disable() {
  document.querySelectorAll('.btn').forEach(el => el.setAttribute('disabled', true));
}
<div id="answer-buttons" class="btn-grid">
  <button class="btn" onclick="disable()">Answer1</button>
  <button class="btn" onclick="disable()">Answer2</button>
  <button class="btn" onclick="disable()">Answer3</button>
  <button class="btn" onclick="disable()">Answer4</button>
</div>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

Replace "disable" with "disabled"

function disable() {
    let elems = document.getElementsByClassName('btn')
    for(let i = 0; i < elems.length; i++){
        elems[i].disabled = true;
    }
}
0

you are using 'disable' instead of 'disabled'
it will be like this

elems[i].disabled = true;
Anonymous Coder
  • 556
  • 2
  • 16
0

Use querySelectorAll to get an array of buttons and it should work

function disable() {
  let elems = document.querySelectorAll('.btn')
  for(let i = 0; i < elems.length; i++){
      elems[i].disabled = true;
  }
}
VeeGee
  • 52
  • 4