How to block all "radios" of a form when you click a button?

3

I made a quiz with some questions and answers, I would like someone to help me solve the following problem, I want that when clicking the button to see the result of quiz , all the answer options will be locked. The user will no longer be able to change their responses.

    
asked by anonymous 20.02.2015 / 06:16

2 answers

3

Just set / change the value disabled of the element to true .

document.getElementById("foo").disabled = true;

Sample code:

var radios = document.getElementsByName('dog');

document.getElementById('block').onclick = function(){ 
    var len = radios.length;
    for(var i = 0; i < len; i++){
        radios[i].checked = false; // caso tenha algum 'radio' marcado
        radios[i].disabled = true; 
    }
}
<button id='block'>Bloquear Formulário</button>

<p><strong>1) Quantas patas tem um cachorro?</strong></p>
<form>
  <input type='radio' name='dog'/>1<br>
  <input type='radio' name='dog'/>2<br>
  <input type='radio' name='dog'/>3<br>
  <input type='radio' name='dog'/>5<br>
  <input type='radio' name='dog'/>Tenho certeza que não são 4<br>
</form>
    
20.02.2015 / 07:38
1

You may also be using it below

Renan Code.

<button id='block'>Bloquear Formulário</button>

<p><strong>1) Quantas patas tem um cachorro?</strong></p>
<form>
  <input type='radio' name='dog'/>1<br>
  <input type='radio' name='dog'/>2<br>
  <input type='radio' name='dog'/>3<br>
  <input type='radio' name='dog'/>5<br>
  <input type='radio' name='dog'/>Tenho certeza que não são 4<br>
</form>

Only the Javascript code will change, I think it's better to do it this way

var radios = document.getElementsByName('dog');

document.getElementById('block').onclick = function(){ 
    radios.forEach(function(el) {
       el.checked = false;
       el.disabled = true; 
    });
}
    
08.11.2016 / 18:21