views:

164

answers:

1

I'm using the Zend framework to display a list of radio options on a html form. I have to use the radio element because i want the user to see all options on the page, i would have used a checkbox otherwise. I'm populating the list of radios from a database query.

$sectorname = new Zend_Form_Element_Radio('sectorname');
$sectorname->setLabel('Sector');
$sectorname->setSeparator(' ');
$sectorTable = new Model_DbTable_Sector();
$sectors = $sectorTable->listSectors();
foreach($sectors as $sector)    
{
    $sectorname->addMultiOption($sector->id,$sector->name);
}
$sectorname->setDecorators($decors);
$this->addElement($sectorname);

$sectorid = new Zend_Form_Element_Hidden('sectorid');
$sectorid->setDecorators($decors);
$this->addElement($sectorid);

The html this is created look like this, you can see that the sector 'id' and 'name' above gets mapped to the label value and input 'id' in html. And there is a hidden sectorid html element.

<tr>
<th>Sector</th>
<td>
<label for="sectorname-1"><input name="sectorname" id="sectorname-1" value="1" type="radio">Accountants</label>
<label for="sectorname-36"><input name="sectorname" id="sectorname-36" value="36" type="radio">Admin</label>
<label for="sectorname-2"><input name="sectorname" id="sectorname-2" value="2" type="radio">Agriculture</label>
<label for="sectorname-5"><input name="sectorname" id="sectorname-5" value="5" type="radio">Architects</label> 
<label for="sectorname-11"><input name="sectorname" id="sectorname-11" value="11" type="radio">Army</label>
<label for="sectorname-48"><input name="sectorname" id="sectorname-48" value="48" type="radio">Undefined</label></td>
<input name="sectorid" value="" id="sectorid" type="hidden">            
</tr>

At the moment, i'm using JQuery to capture the click event on the label/radio element, and update the hidden sectorid value.

->addOnLoad('$("input[name=\'sectorname\']").click( 
 function() 
 { 
  $("#sectorid").val(this.val);
 });');

My problem is the 'sectorname' and 'sectorid' elements will both be numeric values. Is there a way with jquery that i can get the string value of the label element, and set this onto the hidden element in my form?

A: 

Try this

$("#sectorid").val($(this).parent().text());
James Wiseman
nope - the input element doesn't have any text value. the text belongs to the parent label element.
emeraldjava
$(this).parent().text()
emeraldjava