Jquery – Label fires click function twice

clickjquerylabel

Is any way to call a function and at the same time save effect on the radiobutton by clicking label?

Example (I need open/close fieldsets with checkboxes by clicking labels for radiobuttons)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Click on Label</title>
<script src="/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
accordionOptions();
});

function accordionOptions(){
$("label.legend, label.childless").click( function(event){
var opposites = $(".subtype").not($(this).next());
var el = $(this);
opposites.find("input:checked").attr('checked', false);
opposites.removeClass("expanded");
el.siblings("label").removeClass("expanded");
el.next(".subtype").toggleClass("expanded");
el.toggleClass("expanded");
});
}
</script>
<style type="text/css">
<!--
HTML { text-align: center}
BODY { background: #FFF; color: #494949; font: .813em/140% Arial, Helvetica, sans-serif; text-align: left; margin: 4em auto; width: 240px}
LABEL { display: block; margin-bottom: .6em}
.subtype { display: none}
.expanded { display: block !important}
.label.expanded { /*styling selected fieldset heading*/}
-->
</style>
</head>

<body>
<form action="" method="post">
<fieldset>
<label class="legend expanded"><input name="rb_item" type="radio" value="" checked="checked" />Item 0100</label>
<fieldset class="subtype expanded">
<label><input name="item01" type="checkbox" value="" />Item 0101</label>
<label><input name="item01" type="checkbox" value="" />Item 0102</label>
</fieldset>
<label class="childless"><input name="rb_item" type="radio" value="" />Item 0200</label>
<label class="legend"><input name="rb_item" type="radio" value="" />Item 0300</label>
<fieldset class="subtype">
<label><input name="item03" type="checkbox" value="" />Item 0301</label>
<label><input name="item03" type="checkbox" value="" />Item 0302</label>
</fieldset>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>

Best Answer

The problem with e.preventDefault(); is it stops the label click from checking the radio button.

A better solution would be to simply add a "is checked" quick check like so:

$("label").click(function(e){
  var rbtn = $(this).find("input");
  if(rbtn.is(':checked')){
    **All the code you want to have happen on click**
  }
)};