7
I have the code below in HTML:
<input type="checkbox" id="Pacote_i" name="Pacote" value="Pacote i" onClick="soma()">
<input type="checkbox" id="Pacote_ii" name="Pacote" value="Pacote ii" onClick="soma()">
<input type="checkbox" id="Pacote_iii" name="Pacote" value="Pacote iii" onClick ="soma()">
I need to take the values of these fields and create their respective variables. I’m trying with the code below in Javascript, but I’m not getting it. See:
var pacote = document.getElementsByName('Pacote');
for (var i = 0; i < pacote.length; i++){
if ( pacote[i].checked ) {
if(pacote[i].value == "Pacote i"){
var Pacote_i = "Pacote UM";
alert(Pacote_i);
}else if (pacote[i].value == "Pacote ii") {
var Pacote_ii = "Pacote DOIS";
alert(Pacote_ii);
}else if (pacote[i].value == "Pacote iii") {
var Pacote_iii = "Pacote TRÊS";
alert(Pacote_iii);
}
}
}
The code above is correct?
Thank you!
I don’t understand what you want to do, the way you did, when you select the 1, it will create the variable 1, if you click on the 2, will keep the 1 and the 2, why both are checked, if you take some it only goes in what you mateve checked ..
– Renan Degrandi
Yes...exactly what I need to do, but it’s not working.
– user24136
of course it is, I made the test here I selected the 1st showed the Alert package ONE, selected the second, showed the Alert, package ONE, then Package Two, removed the selection of select 1, showed package TWO, that inside the Function (sum) played that its code in the function.
– Renan Degrandi
Code snippet published below for your test
– Renan Degrandi
related, but uses jQuery: http://answall.com/a/12995/13561
– Pedro Sanção