2
I have a checkbox that at the time I select and search the value in the field type="text"
, one of the fields is working perfectly but the second field does not work.
function pega(){
var opcao = document.getElementById("opcao");
var numero1 = document.getElementById("number1").value;
var numero2 = document.getElementById("number2").value;
if (opcao.checked == true){
document.getElementById("total").innerHTML = numero1;
}
/*
if (opcao.checked == true){
document.getElementById("total").innerHTML = numero2;
}*/
}
<input type="checkbox" id="opcao" name="Pacote" value="primeira opcao" onclick ="pega()">
<input type="text" id="number1" />
<input type="checkbox" id="opcao" name="Pacote" value="primeira opcao" onclick ="pega()">
<input type="text" id="number2" />
<div id="total"></div>
My idea later is to calculate the two fields, but I need to know if this way I am doing I will be able?
You’re using the same id for two different elements, don’t do it. Name each one as option 1 and option 2, then check if they are checked, as you are already doing.
– user28595
First, I think you had to accept @Sergio’s reply. Then, why don’t you use jQuery? Why don’t you put Labels us checkbox and takes the value with
$("input[type='checkbox']").val();
or another jQuery variant, such as$(#idCheckbox).val();
? If when selecting a checkbox the other should be unchecked, why not useinput[type="radio"]
?– StillBuggin