I have a very elegant solution using the buttonsetcreate button set built-in event. Alternatively, you can use the button set callback event. All described here https://api.jqueryui.com/buttonset/#event-create
This event will allow you whenever a set of buttons is triggered, you can do whatever you want with it. It is not necessary to check again and again whether this is init or not. Just enter the event code and do it.
the violin
https://jsfiddle.net/ergec/wk9ew1bp/
HTML
<fieldset> <legend>Favorite jQuery Project</legend> <div id="radio"> <input type="radio" id="sizzle" name="project"> <label for="sizzle">Sizzle</label> <input type="radio" id="qunit" name="project" checked="checked"> <label for="qunit">QUnit</label> <input type="radio" id="color" name="project"> <label for="color">Color</label> </div> </fieldset> <button id="initbuttonset">Init</button> <button id="checkbuttonset">Check</button>
Js
var isbuttonsetinit = false; $("#radio").on("buttonsetcreate", function(event, ui) { isbuttonsetinit = true; alert("buttonset init"); }); $("#initbuttonset").click(function() { $("#radio").buttonset(); }); $("#checkbuttonset").click(function() { alert(isbuttonsetinit); });
js (alternative)
var isbuttonsetinit = false; $("#initbuttonset").click(function() { $("#radio").buttonset({create: function( event, ui ) { isbuttonsetinit = true; alert("buttonset init"); }}); }); $("#checkbuttonset").click(function() { alert(isbuttonsetinit); });
Ergec source share