2
my question is, how to create a button that randomly and automatically selects 3 of the 5 numbers that are shown. Like I just clicked on them.
follows all my code below.
<html>
<head>
<style type="text/css">
input[type=checkbox] {
display:none;
}
input[type=checkbox] + label {
color: black;
border: 1px solid #000;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
}
input#input_1_1:checked + label {
color: white;
background-color:#4682B4;
font-weight: 500;
border: 1px solid #000;
}
input#input_1_2:checked + label {
color: white;
background-color:#4682B4;
font-weight: 500;
border: 1px solid #000;
}
input#input_1_3:checked + label {
color: white;
background-color:#4682B4;
font-weight: 500;
border: 1px solid #000;
}
input#input_1_4:checked + label {
color: white;
background-color:#4682B4;
font-weight: 500;
border: 1px solid #000;
}
input#input_1_5:checked + label {
color: white;
background-color:#4682B4;
font-weight: 500;
border: 1px solid #000;
}
</style>
</head>
<body>
<input type="text" id="resultado">
<input type='checkbox' name='q1_myOptions[]' onclick="add(this)" value="01" id="input_1_1"/>
<label for="input_1_1">01</label>
<input type='checkbox' name='q1_myOptions[]' onclick="add(this)" value="02" id="input_1_2"/>
<label for="input_1_2">02</label>
<input type='checkbox' name='q1_myOptions[]' onclick="add(this)" value="03" id="input_1_3"/>
<label for="input_1_3">03</label>
<input type='checkbox' name='q1_myOptions[]' onclick="add(this)" value="04" id="input_1_4"/>
<label for="input_1_4">04</label>
<input type='checkbox' name='q1_myOptions[]' onclick="add(this)" value="05" id="input_1_5"/>
<label for="input_1_5">05</label>
<script>
function add(_this){
var resultado = document.getElementById('resultado');
var value = _this.value;
var hasAdd = resultado.value.search(_this.value) > 0
if(_this.checked && !hasAdd){
resultado.value += ' '+_this.value;
}else if(!_this.checked && hasAdd){
var er = new RegExp(_this.value, 'ig');
resultado.value = resultado.value.replace(er, '');
}
resultado.value = resultado.value.replace(/ {2,}/g, ' ');
}
</script>
</body>
</html>
Interesting way to use spread to array the elements.
– dsantoro
@dsantoro It is indeed not detailed that part in the answer, but it is one of the most direct ways to convert from a
NodeList
which is what comes out ofquerySelectorAll
forArray
. Although many things are possible to use directly in aNodeList
such as aforEach
some not so much as thesplice
– Isac
always used this way: var myVar = [].slice.call(Document.querySelectorAll('selector')); but this approach is interesting, but only used with some transpiler right.
– dsantoro
@dsantoro It will end up being the same. The way I used it is a little more ES6 and shorter, but it remains native js.
– Isac