I want to use Bootstrap 4 buttons to indicate user choices on a page. The buttons should toggle selected/unselected and I'll need to be able to read their state as they're changed. I'm using on('click') to collect the state of the buttons. The results seem like I've got a timing issue. When a button is clicked it accurately reports the settings of all the buttons except the one clicked. A separate button setup just to report the button states always reports correctly. How do I correctly acquire the button states when one of the buttons have been pressed?
function showSelection() {
jQuery("#selection_display").empty();
jQuery("#selection_display").append("<hr>choice1 active:" + jQuery("#choice1").hasClass('active'));
jQuery("#selection_display").append("<hr>choice2 active:" + jQuery("#choice2").hasClass('active'));
jQuery("#selection_display").append("<hr>choice3 active:" + jQuery("#choice3").hasClass('active'));
}
jQuery(document).ready(function () {
jQuery(".option_group1").on("click", function () {
showSelection();
});
});
<button id="choice1" data-toggle="button" class="btn btn-secondary option_group1">choice1</button>
<button id="choice2" data-toggle="button" class="btn btn-secondary option_group1">choice2</button>
<button id="choice3" data-toggle="button" class="btn btn-secondary option_group1">choice3</button>
<button onclick="showSelection();">Update Selection</button>
<div id="selection_display">
</div>