Add data attributes on <options> using form_dropdown() |
How can I add data attributes in the <option> tags using the form_dropdown() helper ? To generate something like this, for example:
PHP Code: // the part that I'm interested is the [b]data-code[/b] attribute
Hello ,
This can be done in codegniter : form_dropdown('project_interested_in', array("" => "Location Interested In") + $arrProjectNameCity, "", ' id="project_interested_in" class="form-control" ')
If you use CI form_dropdown then you can pass it an array of options.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(08-20-2015, 04:17 AM)InsiteFX Wrote: If you use CI form_dropdown then you can pass it an array of options. I'm using CI form_dropdown, but from what I see in the documentation it gives only an example of how to pass the options as a key => value array, like this PHP Code: <option value="$key" data-attr="???">$val</option> What about the data attributes??
You use the $extra
PHP Code: $extra = 'id="1" class="class"'; What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
The form_dropdown() function doesn't have anyway to add attributes to the option elements. $extra is added to the select element.
If you pass a two-dimensional array to $options, it creates an optgroup element with the interior key/value pairs used for option elements inside the optgroup. The best you could probably do if you still want to use form_dropdown() would be to create a new version of the function in MY_form_helper.php which takes an additional parameter with an array for the option attributes. In that case, I would recommend setting it up to require the same keys used in the $options array with the value being an array or string that can be passed to _attributes_to_string() (like the $extra parameter). Here is an example. Most of the code here is just copied from the form_helper, and, other than the added parameter on the first line, I've marked the changed lines with comments indicating the changes. Note that although it supports adding attributes to option elements within optgroup elements, it doesn't support adding attributes to the optgroup elements themselves. PHP Code: function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '', $optionsExtra = array()) |
Welcome Guest, Not a member yet? Register Sign In |