Assign from dropdown list default value

Ivan Louw asked on March 2, 2018 05:54

Hi,

I have 2 drop down list. When selecting certain values on the first drop down the drop down list enables the second drop down list (I have not been able to find where it enables the second drop down list as I still new to Kentico, it is not using the visibility or enabled conditions.)

Both list are have a "Select Value" as default with a rule requiring that a value be chosen in the dropdown list and both are required.

I tried to assign a default value to the second list based on the selected value on the first list. {% (if (ContactReason.Value == "Test"){ "1" } else { "Test this" } %} But the second list rule is still executed even when drop down list is not enabled. This prevent the form from being submitted.

How can I change the selected value in the second list based on a value selected in the first list so that the rule in the second dropdown list is not preventing the page from being submitted.

Recent Answers


Trevor Fayas answered on March 2, 2018 20:20

This can depends a lot of where the drop downs are and what they are.

So first need some context:

  1. Are these drop downs in a page, or being used in a user interface within Kentico
  2. Are the drop downs asp:dropdown controls, are they just HTML being rendered, or something else?
  3. When you select the first drop down, is there a post back, JavaScript event, or what is triggering so you can try to alter the other.

Thanks!

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on March 5, 2018 05:36

Hi Trevor,

  1. The drop downs are in a form that is used in a page.
  2. They are HTML rendered.
  3. Found a widget in the form at the end with JavaScript in that enables and disables the drop downs. Tried the following $("#Test").val('Test This'); But it seems the value is being overwritten by the default value or not being hit??

Thanks

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 5, 2018 06:22

You're close. Since you want to set the selected drop down after the first, you can't really use the Default Value. So for your first drop down, click the advanced option in the Drop DOwn form control, and you'll see an OnChange Client Script.

Set it to this:

$jq = (typeof $cmsj == "undefined" ? $ : $cmsj); $jq("select[id*='MySecondDropDownFieldName']").val($jq(this).val() == "Test" ? "1" : "Test This");

The first part is because inside the CMS there is no $, but there is $cmsj, so this just makes sure to grab either $ or $cmsj whichever is defined. Not totally necessary but without it this logic won't work within the CMS, only on the website portion.

The rest is pretty simple, it uses an ID wild card lookup to grab the 2nd drop down's select and uses this (the first drop down)'s value to set the other. Tested and worked for me.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.