javascript - Error with Changing Variable Value with if Statement -
i have radio buttons in accordion, , depending on radio buttons selected, alert different output. (there's ton of css)
var acc = document.getelementsbyclassname("accordion"); var i; (i = 0; < acc.length; i++) { acc[i].onclick = function(){ if(!this.classlist.contains("active")) { closeaccordions(); } this.classlist.toggle("active"); this.nextelementsibling.classlist.toggle("show"); } } function closeaccordions() { (i = 0; < acc.length; i++) { acc[i].classlist.remove("active"); acc[i].nextelementsibling.classlist.remove("show"); } } var chanceoflive5 = 1; var user; function choose(choice){ user = choice; } function changechanceoflive2(){ if (user == 'bolts') { chanceoflive5 = 1; } else if (user == 'plates') { chanceoflive5 = 2; } } var chanceoflive6 = 1; var user2; function choose2(choice2){ user = choice2; } function changechanceoflive3(){ if (user2 == 'tie') { chanceoflive6 = 1; } else if (user2 == 'struts') { chanceoflive6 = 2; } } function nextpage(){ var chanceoflive7 = chanceoflive5 + chanceoflive6; alert(chanceoflive7); }
button.accordion { background-color: #eee; color: #444; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; } button.accordion.active, button.accordion:hover { background-color: #ddd; } button.accordion:after { content: '\002b'; color: #777; font-weight: bold; float: right; margin-left: 5px; } button.accordion.active:after { content: "\2212"; } div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } div.panel.show { opacity: 1; max-height: 500px; } body { font-family: sans-serif; font-weight: normal; margin: 10px; color: #999; } form { margin: 40px 0; } div { clear: both; margin: 0 50px; } label { border-radius: 3px; border: 1px solid #d1d3d4 } /* hide input */ input.radio:empty { margin-left: -999px; } /* style label */ input.radio:empty ~ label { position: relative; float: left; line-height: 2.5em; text-indent: 3.25em; margin-top: 2em; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } input.radio:empty ~ label:before { position: absolute; display: block; top: 0; bottom: 0; left: 0; content: ''; width: 2.5em; background: #d1d3d4; border-radius: 3px 0 0 3px; } /* toggle hover */ input.radio:hover:not(:checked) ~ label:before { content:'\2714'; text-indent: .9em; color: #c2c2c2; } input.radio:hover:not(:checked) ~ label { color: #888; } /* toggle on */ input.radio:checked ~ label:before { content:'\2714'; text-indent: .9em; color: #9ce2ae; background-color: #4dcb6d; } input.radio:checked ~ label { color: #777; } button.accordion { background-color: #eee; color: #444; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; } button.accordion.active, button.accordion:hover { background-color: #ddd; } button.accordion:after { content: '\002b'; color: #777; font-weight: bold; float: right; margin-left: 5px; } button.accordion.active:after { content: "\2212"; } div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } div.panel.show { opacity: 1; max-height: 500px; } body { font-family: sans-serif; font-weight: normal; margin: 10px; color: #999; } form { margin: 40px 0; } div { clear: both; margin: 0 50px; } label { border-radius: 3px; border: 1px solid #d1d3d4 } /* hide input */ input.radio:empty { margin-left: -999px; } /* style label */ input.radio:empty ~ label { position: relative; float: left; line-height: 2.5em; text-indent: 3.25em; margin-top: 2em; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } input.radio:empty ~ label:before { position: absolute; display: block; top: 0; bottom: 0; left: 0; content: ''; width: 2.5em; background: #d1d3d4; border-radius: 3px 0 0 3px; } /* toggle hover */ input.radio:hover:not(:checked) ~ label:before { content:'\2714'; text-indent: .9em; color: #c2c2c2; } input.radio:hover:not(:checked) ~ label { color: #888; } /* toggle on */ input.radio:checked ~ label:before { content:'\2714'; text-indent: .9em; color: #9ce2ae; background-color: #4dcb6d; } input.radio:checked ~ label { color: #777; }
<hr> <button class="accordion">foundation bolting</button> <div class="panel"> <div> <input type="radio" name="radio" id="radio1-1" class="radio" onclick="choose('bolts')" checked/> <label for="radio1-1">foundation bolts</label> </div> <div> <input type="radio" name="radio" id="radio1-2" class="radio" onclick="choose('plates')"> <label for="radio1-2">foundation plates</label> </div> </div> <hr> <button class="accordion">wall bracing</button> <div class="panel"> <div> <input type="radio" name="radio2" id="radio2-1" class="radio" onclick="choose2('tie')" checked/> <label for="radio2-1">strong tie retrofit connectors</label> </div> <div> <input type="radio" name="radio2" id="radio2-2" class="radio" onclick="choose2('struts')"> <label for="radio2-2">angled iron struts</label> </div> </div> <hr> <form action=""> <div class="wrapper"> <button class="button" onclick="changechanceoflive2(); changechanceoflive3(); nextpage()" align=center>submit</button> </div> </form>
when click bottom option each list, should alert 4, instead, alerts 3. why?
Comments
Post a Comment