Question

I am trying to do something interesting with jquery and my forms. And it all boils down to being able to group elements together based on specific attributes. I have a multicheckbox with a bunch of options in it which is generating the following example output:

<label for="xyz-nn">
 <input type="checkbox" value="nn" id="xyz-nn" name="xyz[]">Doodle
</label>

Imagine that lots of times. Anywho, I want to add an attribute with a unique value to each checkbox, but using setAttrib("attributeName", "valueN") sets the attribute as the last value instead of a new value for each checkbox.

The question is: how do I set a unique value for an attribute on each option?

Was it helpful?

Solution

A bit of a naught way to do this.

I can't set different attributes to each multioption - the last one will always over-ride an existing attribute. So I've had to put a class around the text for the checkbox. Easier said than done when there is not a clear way to do it.

First, we have to disable escaping. To do this, we have to set the escape attribute as boolean false:

$element->setAttrib('escape', false);

This allows us to put in our own special brand of html.

$element->addMultiOption($object->id, '<span class="' . $otherObject->id . '">' . $object->name) . '</span>';

The output for an option is something like:

<label for="object-1">
  <input type="checkbox" value="1" id="object-1" name="objects[]" />
    <span class="food">Object One</span>
</label>

With each option now with a unique, I can group them however which way I want using jQuery :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top