1

I have two Joomla list fields (drop-down fields). The the second list field is depending on the value of the first list field.

Now I've tried to change the values of the second form field via jQuery on runtime. But this methode fails, because the value can be seen by the browser inspector, but it wont be shown in the form field (seems that Joomla has its own method to display option fields).

I've tried:

$document->addScriptDeclaration('
    jQuery(document).ready(function () {
        jQuery("#jform_country").change(function(){
            country_id = jQuery("#" + this.id).val(); // this is the first list field
            select = document.getElementById("#jform_state");
            if (country_id == "Germany") {
                // Option 1 will be shown, but only in the inspector
                jQuery("#jform_state").append("<option value=Bavaria>Bavaria</option>");
                // Option 2 nothing happens
                var option = document.createElement("option");
                option.text = "Bavaria";
                option.value = "Bavaria";
                select.appendChild(option);
            }    
        })
    });
');

Is there a possibility to fill the list field on runtime resp. to change the values?

1 Answer 1

3

If you are using "Chosen" for your selects then you should trigger "Chosen" to update the data:

jQuery("#jform_state").append("<option value=Bavaria>Bavaria</option>");
jQuery("#jform_state").trigger("liszt:updated");

The most recent version of "Chosen" uses a slightly different event:

trigger("chosen:updated");

But in Joomla 3 (at the moment of writing up to 3.4) the version of "Chosen" is older, so you should use:

trigger("liszt:updated")
3
  • good solution! But now I face the problem, that the value will be shown and after saving, the value will be also stored in the database, but at new open of the site the value from the database won't be shown in the field. Is this because of no standard values in the xml formfield description?
    – Perino
    Feb 17, 2015 at 13:47
  • Please open a new question. Feb 17, 2015 at 15:33
  • New thread has been opened under joomla.stackexchange.com/questions/8663/…
    – Perino
    Feb 17, 2015 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.