I've a much elegant solution using builtin event buttonsetcreate
of buttonset. Alternatively you can use callback event of the buttonset. All described here https://api.jqueryui.com/buttonset/#event-create
This event will allow you whenever the buttonset initiated, you can do whatever you want with it. No need to check again and again to see if it is init or not. Just put your code in event
and be done.
fiddle
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);
});