Milind Milind - 4 months ago 17
Smarty Question

How to add title attribute in drop down using {html_options} in smarty 3.1

Here is my code.

PHP CODE

$smarty->assign('myOptions', array(
1800 => 'ABC',
9904 => 'DEF',
2003 => 'GHI')
);
$smarty->assign('mySelect', 9904);


SMARTY CODE

{html_options name=foo options=$myOptions selected=$mySelect}


DESIRED HTML OUTPUT

<select name="foo">
<option value="1800" title="ABC">ABC</option>
<option value="9904" title="DEF" selected>DEF</option>
<option value="2003" title="GHI">GHI</option>
</select>

Answer

You can't; not with the default html_options function at least. You can try to modify it or better create your own plugin to allow more parameters or complex arrays, but other than that, the best way to do it is generating the list of options in the template using a foreach.

Another option would be to use javascript to add the title attribute to each <option> for this or all selects once the page has been loaded.