pre-check items of form checkbox or picklist


In a From, I have an 'Eval populate' code to populate my checkbox list or picklist.
I would like to pre-check some items.

How can I do ?
I think you should be able to do it already within your eval code (didn't test though).

Just check if checkbox value is "something". If yes, add "checked" attribute to the checkbox element.
@juuser Doesn't eval populate just expect an array (options) for the values? How can an attribut be set for one of the options?
Assuming the OP uses similar code:
$db = JFactory::getDbo();
$db->setQuery('SELECT id, text FROM #__tablename');
$rows = $db->loadObjectList();
foreach ($rows as $row) {
$options[] = JHTML::_('select.option', $row->id, $row->text);
return $options;
I remember that the JHTML accepted third parameter (array) as an argument, like:
$options[] = JHTML::_('select.option', $row->id, $row->text, 'ARGUMENTS ARRAY');
but now that I tested that, it indeed doesn't seem to work.

So @wezetel's suggestion is probably a way to go. Just some generic js/jQuery should do in form_x.js which loops through the checkbox options on "document ready" function and sets the option as checked if the value is "something".
Try assigning the output of the JHTM to a var of $option. The set $option->selected = 'selected'; (might be 1 or true, don't remember off the top of my head), then $options[] = $option, I am going from old memory here but I hope it helps.
Thank's for the answer.
I don't find how to insert the selected option.

part of eval code :

$Db->setQuery("SELECT id,nom FROM table");
$Libs = $Db->loadObjectList();
foreach ($libs as $Lib) {
$options[] = JHTML::_('select.option', $Lib->id, $Lib->nom);
return $options;
I'm sorry but I don't understand what you explain.
Can you tell me what I have to change in my following script ?
In F3, the 'echo' with 'script' toggle the item I want to pre-check.

eval script :

$Db = FabrikWorker::getDbo(false, 2);
$Db->setQuery("SELECT id,name,select FROM name_table WHERE `type`='Y' ORDER BY name");
$Lines = $Db->loadObjectList();
foreach ($lines as $line) {
$options[] = JHTML::_('select.option', $line->id, $line->name);
if ($line->select == 'Y') {
echo '<SCRIPT>tableau_check.push("fabrikgrid_'.$line->id.'");</SCRIPT>';
return $options;
What do you want to achieve at all
  • Set defaults in a new record?
  • Override the stored data in an existing record?
We are in need of some funding.
More details.

Thank you.

Members online