Forums

Linked dropdowns

awgcas 18 Nov, 2023
Hi, I have 2 dropdowns, with same values read from a db table. When I select an option from the first one, I should like to have the same option disabled (or removed) in the second dropdown.
I tried something like this:

$('select[name=first]').on('change', function() {
var self = this;
$('select[name=second]').find('option').prop('disabled', function() {
return this.value == self.value
});
});

where first and second are names of dropdowns, but it doesn't work ...
Any suggestion?
Thank you
Alex
Max_admin 19 Nov, 2023
this code uses jQuery but v8 does not use jQuery, so unless your page has jQuery loaded then it will not work, you can check the browser's console for errors
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
awgcas 19 Nov, 2023
Hi Max, to load jquery I have a php action including:
use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->useScript('jquery');

In the console I have no errors.

Thank you
Alex
Max_admin 19 Nov, 2023
ok, does the change event fire in your code ? I think your code should be inside a document ready event for it to work
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
awgcas 20 Nov, 2023
Thank you Max, now it (partially) works.

I'm using this code:

var $dropdown1 = $("select[name='first']");
var $dropdown2 = $("select[name='second']");

$dropdown1.change(function() {
    $dropdown2.empty().append($dropdown1.find('option').clone());
    var selectedItem = $(this).val();
    if (selectedItem) {
        $dropdown2.find('option[value="' + selectedItem + '"]').remove();
    }
});


because in my opinion remove option is better than disabling.

The problem now is that the option is correctly removed "from the code" (as I can see in firefox dev tools), but it is still visible in the dropdown, because it is read from the database (I guess).

I'm not a developer, but if I'm not wrong I have to remove that option from the array of vars, so it won't appear in the dropdown...

Thanks again
Alex
Max_admin 20 Nov, 2023
what happens when the user "unselects" an option from the first one ? will this readd the option to 2nd one ?

I think that you better use 2 radio groups or checkboxes groups, that will be easier to "hide" and show
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
awgcas 20 Nov, 2023
Most probably the options in the dropdown will be more that 10 , that's why I prefer it.
I made more tests: even deleting "Dynamic options" in the dropdown behavior, and creating static options, the option is still correctly removed "from the code", but visible in the dropdown; so the problem is not depending from the dynamic creation of options.

If you want, the form is available on http://131.114.28.116/index.php/test, with debug activated.
One more question: in the Vars block after submit, I have the following array (result of a read data action):

[read_data9] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [partenza] => Posto 1
                    [arrivo] => Posto 2
                    [costo] => 12
                )

        )


How can I echo via php (or html) the [costo] row?

Thank you
You need to login to be able to post a reply.