I have a Zend form that has two checkboxes, if the first is unchecked the second should be disabled.
The two elements are:
$element = $this->createElement('checkbox','show_assessments');
$element->setLabel('Do you want to view assessments?');
$element->setAttrib('onchange', 'javascript:toggleElement("show_assessments", "show_expired")');
$element->clearDecorators();
$element->addDecorator('StandardTable');
if($preferences['show_assessments'])
$element->setValue(1);
$this->addElement($element);
$this->_elementNames[] = 'show_assessments';
$element = $this->createElement('checkbox','show_expired');
$element->setLabel('Include expired programs?');
if(!$preferences['show_assessments'])
$element->setAttrib('disable', true);
$element->clearDecorators();
$element->addDecorator('StandardTable');
if($preferences['show_expired'])
$element->setValue(1);
$this->addElement($element);
$this->_elementNames[] = 'show_expired';
and my Javascript function to toggle the elements is:
function toggleElement(sender, receiver)
{
if (!sender || !receiver) return;
if ($('#'+sender).is(':checked'))
{
$('#'+receiver+' :input').removeAttr('disabled');
alert(receiver+' is now enabled');
}
else
{
$('#'+receiver+' :input').attr('disabled', true);
alert(receiver+' is now disabled');
}
if ($('#'+receiver).is('checked'))
alert(receiver+' is checked');
else
alert(receiver+' is not checked');
}
The problem is that the last if statement, checking if receiver is checked, is false no matter what, and receiver is never toggled. Any ideas?
Edit:
It seems to always have a problem with the second element. I tried reversing it, so show_expired was the sender and show_assessments was the receiver and this time it could tell when show_expired was checked, but couldn’t tell for show_assessments.
Got it to work without jQuery