EDIT:
I have figured out what was going on with my problem. I set dependant and depending fields for the dropdowns so it does a page refresh which wipes away the previous changes done so in my case instead of doing the hide/show in a .change function just move that code into document.ready when the page is finished loading.
Another problem persists is that the values being saved into my database tables are the ID value of the dropdown list instead of the text? Is there any way to have it store as text instead?
Hi,
I've been playing around with dropdown form controls recently with custom macro methods, custom macro mdoules, and custom table.
I've come to the conclusion to use a custom table instead to avoid all the code behind and make it easier to make changes strictly from kentico side. I have a cascading dropdown, the 2nd dropdown gets populated by a SQL datasource something like SELECT Value, Name FROM custom_table. I then filter it with javascript in the layout section of the form.
My Problem:
I can't seem to get the javascript working to hide the dropdown list options based on the my 1st dropdown list selection. I've noticed the dropdown lists are wrapped inside a div so I grab the div first and then it's select element inside the div.
Javascript:
<script type="text/javascript">
$(document).ready(function () {
$divDdlOne = $("[id$='_ncpddlone'] select"); // will get the select element of the dropdown
$divDdlTwo = $("[id$='_ncpddltwo'] select");
$($divDdlone).change(function() {
var selectedOption = $(this).val();
if(selectedOption.indexOf("value 1 from ddl one") != -1) {
console.log("Got value 1 from ddl one");
$($divDdlTwo).find("option[value='3']").hide();
}
});
});
</script>
So based on the javascript, I have console.log in there to test if it actually goes into the if statement and it does. I even console.logged $(DivddlTwo).find("option[value='3']")val() before and it gives me back the correct value so I would assume that it's actually accessing the proper value in my dropdown list. The issue is it never hides that value, so I can't filter my ddl two properly based on the selected value of the first dropdown.
Any help would be appreciated thanks!