I am trying to use Twitter Bootstrap button group with data-toggle="buttons-radio" on my site. Boot markup as follows.
<div class="btn-group program-status" data-toggle="buttons-radio"> <button class="btn">All</button> <button class="btn">Active</button> <button class="btn">Planning</button> <button class="btn">End of Life</button> <button class="btn">Cancelled</button> </div>
I need to redirect to the same page with the request depending on the button clicked. I tried using the following jQuery code for this.
<script> var sParamStr = ''; function addToParamStr(str) { sParamStr += str; } function redirectToUpdatedLocation() { $('.program-status > .btn.active').each(function () { addToParamStr( '?status=' + $(this).text()); }); console.log(sParamStr); window.location.href = "program" + sParamStr; } $document.ready(function () { $('.program-status > .btn').on('click', function (e) { redirectToUpdatedLocation(); }); }); </script>
But the browser always redirects to {site} / program without a query string. Commenting out the line window.location.href = "program" + sParamStr; I managed to notice that the second click forward, sParamStr will be added correctly.
It seems like my code is trying to read the text of the pressed button before it has finished the form of the form .button('toggle') formstock.js. The code worked as intended when I changed the function as follows.
$document.ready(function () { $( '.program-status > .btn').on('click', function (e) { $(this).addClass('active'); redirectToUpdatedLocation(); }); });
While this method works for me right now, I would like to know the right way to achieve this. ie How do I execute my code after the last click binding?
UPDATE:
I found this link in the Twitter Bootstrap forum. This seems to be a known issue. https://github.com/twitter/bootstrap/issues/2380
source share