views:

46

answers:

1

I would like to add the id attribute to a label field in a Zend Form Element, but when setting 'id'=>'namelabel' in the decorator properties, it sets for='namelabel' in the label instead of adding the attribute of id='namelabel'

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Business Name:');
$name->addDecorator('Label',array('class'=>'form_label', 'id'=>'namelabel'));
$name->addDecorator('HtmlTag',array('tag'=>'span','class'=>'form_inputs'));
$name->setOrder(1);
$name->size='40';

renders

<label for="namelabel" class="form_label optional">Business Name:</label>

when I want it to render

<label id="namelabel" for="name" class="form_label optional">Business Name:</label>

Is this even possible?

A: 

What ZF is doing is correct, because the for in your label should be the same as the id in your input. So you should not change this to be something different.

Have you tried:

$name->addDecorator('Label',array('class'=>'form_label', 'id'=>'name'));

More importantly, why do you need to do this?

Edit after OP response

The for should be unique enough for this, you can change your ZF code to the below, as you do not need to set an id

$name = new Zend_Form_Element_Text('zf_element_name');
$name->setLabel('Business Name:');
$name->addDecorator('Label',array('class'=>'form_label'));
$name->addDecorator('HtmlTag',array('tag'=>'span','class'=>'form_inputs'));
$name->setOrder(1);
$name->size='40';

Then your javascript, if your using jQuery you can do this

$("label[for='zf_element_name']").html("Some new Label text");
jakenoble
I understand the for should be the same as the id of the input, which is why I still want the for attribute. The reason I want a unique id attribute on my label tag is so that I can change the innerHTML on a javascript call
Rob
I have edited my answer for you
jakenoble
Thanks very much for your response. Unfortunately I'm using prototype and not jQuery, is there a similar function in prototype I can use?
Rob
well I've found I can do a similar thing in prototype with var e = $$("label[for='name']");var label = e[0];label.innerHTML = "Some new Label text";
Rob
Good work Rob!!
jakenoble