I have a form setup so that when you check a checkbox, the relevant UL will expand showing more options based on the choice. It works, but only when the user clicks the label of the checkbox. If the checkbox is clicked, the UL expands as expected, but the box doesn't get checked.
Javascript:
jQuery(document).ready(function()
{
jQuery('.toggleUL').click(function()
{
var checkbox = jQuery(this).find('input');
var ul = jQuery(this).next('ul');
if (ul.css('display') == 'block') { ul.find('input').attr('checked', false); }
if (checkbox.attr('checked') == false)
{
checkbox.attr('checked', true);
}
else
{
checkbox.attr('checked', false);
}
ul.slideToggle('slow'); return false;
});
});
HTML:
<label class="toggleUL"><input style="margin-right: 10px" type="checkbox" name="quoteWebsite" id="quoteWebsite" value="xxx" />xxx</label>
<ul style="list-style-type: none; display: none">
<li style="margin-left: 20px"><label><input style="margin-right: 10px" type="checkbox" name="quoteWebsite1[]" id="quoteWebsite1" value="xxx" />xxx</label></li>
<li style="margin-left: 20px"><label><input style="margin-right: 10px" type="checkbox" name="quoteWebsite1[]" id="quoteWebsite2" value="xxx" />xxx</label></li>
<li style="margin-left: 20px"><label><input style="margin-right: 10px" type="checkbox" name="quoteWebsite1[]" id="quoteWebsite3" value="xxx" />xxx</label></li>
</ul>
The slideToggle() function wasn't working the way it should. When the box/label was clicked, the UL would expand then retract instantly. The only way I could get it to stop was to add 'return false;' which, of course, causes the checkbox not to check. Hence the checkbox.attr().
Hacked up code, I know. But how can I get the checkbox to work? >.<