I am using jQuery to create custom radio buttons and I have a problem. When I click on the label associated with the radio, the click events fire twice, if I click only on the radio, it works fine (well, actually, this is not the radio that I click, but the div that wraps the entire input and label). Here is the code:
HTML:
<div id="box"> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem>RADIO1</asp:ListItem> <asp:ListItem>RADIO2</asp:ListItem> <asp:ListItem>RADIO3</asp:ListItem> </asp:RadioButtonList> </div>
JQuery
<script type="text/javascript"> $(function () { $('#box').find('input:radio').each(function (i) { var input = $(this); // get the associated label using the input id var label = $('label[for=' + input.attr('id') + ']'); // wrap the input + label in a div $('<div class="custom-radio"></div>').insertBefore(input).append(label, input); var wrapperDiv = input.parent(); // find all inputs in this set using the shared name attribute var allInputs = $('input[name=' + input.attr('name') + ']'); // necessary for browsers that don't support the :hover pseudo class on labels label.hover( function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover checkedHover'); }); //bind custom event, trigger it, bind click,focus,blur events wrapperDiv.bind('updateState', function () { if ($(this)[0].children[1].checked) { allInputs.each(function () { var curDiv = $('div > label[for=' + $(this).attr('id') + ']').parent(); curDiv.removeClass('custom-radio-checked'); curDiv.addClass('custom-radio'); }); $(this).toggleClass('custom-radio custom-radio-checked'); } else { $(this).removeClass('custom-radio-checked checkedHover checkedFocus'); } }) .trigger('updateState') .click(function () { console.log('click'); }) .focus(function () { label.addClass('focus'); }).blur(function () { label.removeClass('focus checkedFocus'); }); }); }); </script>
Is there any solution for this behavior?
jquery
tone Nov 23 '11 at 7:31 2011-11-23 07:31
source share