• Joomla 5.1

    For running J!5.1 you must install Fabrik 4.1
    See also Announcements

  • Subscription and download (Fabrik 4.1 for J!4.2+ and J!5.1) are working now

    See Announcement
    Please post subscription questions and issues here

    We have resolved the issue with the J! updater and this will be fixed in the next release.

[SOLVED] Add option to dropdown (select) via JS

genyded

Active Member
Hi,

Does anyone know of a way to add options to a Fabrik form dropdown element via Javascript? Using .add on the element does not work and neither does creating a JS "option" and trying to add it.

TIA,
Dale
 
You can do something like that:
1) Create hidden <select> with the desired options:
Code:
<select class="a1" style="display: none;">
    <option>Option 1</option>
    <option>Option 2</option>
</select>
2) Append your custom options to your dropdown element
Code:
$('.a1 option').appendTo('#your_table___dropdown_element_name');
3) Remove hidden <select> from DOM
Code:
$('a1').remove;
And that is it!
 
Thanks,

It took some serious trial and error, but I got this working with an easier solution. Here is the dumbed down code (I am actually getting the values from a web service and adding them in a loop, but left that out for brevity):

Code:
var folders = $('ep_conference___xcheck_folders');
 
//this just removes and existing values if needed
while (folders.options.length > 0) {
        folders.remove(folders.options.length - 1);
}
 
var opt = document.createElement("option");   
opt.text = "testme";
opt.value = "metoo";
folders.add(opt, null);

Dale
 
Back
Top