/// Create a variable to track the group number attribute
var nGroupAttribute = 0;
/// Get the question
var oQuestion = wscScripting.getQuestionByDataPipingCode('SINGLESELECT');
/// Loop through the table elements
$('#' + oQuestion.identity + '_table > tbody > tr').each(function(index, tr) {
/// Create the button template
var sButton = '<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td align="center" class="butt_l_c"><img alt="" src="/images/1pix.gif"></td><td align="center" class="butt_m_c"><a href="#" id="groupbuttonNNNN" group="NNNN" class="butt_m">CHOICETEXT</a></td></tr></tbody></table';
/// Find if this is a title
if ($(tr).find('.wsc_radiofield-title').length > 0) {
if ($(tr).children('td').length > 0) {
/// Increment the attribute so we can find it
nGroupAttribute++;
/// Get the group title text from the span element
var sText = $(tr).find('span').html();
/// Replace the button placeholder text with the Group Title text
var sNewButton = sButton.replace('CHOICETEXT', sText);
/// Replace the group number attribute in the button template
sNewButton = sNewButton.replace(/NNNN/g, nGroupAttribute.toString());
/// Update the table html with the new button template
$(tr).children('td').html(sNewButton);
/// Adjust the width style
$('#groupbutton' + nGroupAttribute.toString()).css('width','200px');
/// Add a click event
$('#groupbutton' + nGroupAttribute.toString()).on('click', function(e, o) {
/// Find the attribute group
var sGroup = $(this).attr('group');
/// Show the group
$("tr[groupall='" + oQuestion.identity + "']").hide();
$("tr[group='" + sGroup + "']").show();
/// Return false so it doesnt affect the url
var oSelected = wscScripting.getFirstSelectedChoice(oQuestion);
if (oSelected != null) {
/// Make sure the choice is one of the choices in the current group or deselect it
var bSelected = false;
$("tr[group='" + sGroup + "'] input").each(function(index, input) {
var sValue = $(input).val();
if (sValue == oSelected.identity) {
bSelected = true;
}
});
if (!bSelected) {
wscScripting.deselectChoice(oQuestion, oSelected);
}
}
return false;
});
}
}
else {
/// Hide groups by default
$(tr).attr('group',nGroupAttribute.toString());
/// Add an attribute so we can find them all
$(tr).attr('groupall',oQuestion.identity);
$(tr).css('display','none');
}
});