Skip to content
Advertisement

Show dropdown value based on previous selection

Is it possible for me to only display the related data on my second dropdown option when I click it on my first dropdown? I saw a similar question on stackoverflow, but I can’t get it to work. The link is available here. It works when I copy and paste the code from the solution into my system. However, when I tried it on my code, it did not work.

Here’s the flow: If DEPARTMENT is equals to Grade School then Grade & Section, dropdown will only show the Kindergarten to Grade 6 and then if DEPARTMENT is equals to Junior High School, dropdown will only shows Grade 7 to Grade 10 while if DEPARTMENT is equals to Senior High School, dropdown will only shows Grade 11 to Grade 12.

User interface: enter image description here

Here’s the code that I’m using..

JavaScript

Here’s the screenshot of my database.

enter image description here

Advertisement

Answer

A quick fix to a minor problem

The problem is a very minor one that is difficult to spot, but very easily fixed.

The department field values in your database are uppercase, but the script tries to match to lowercase. So there is never a match and script hides all of your grade options.

To fix this problem, change the following line of code as shown:

JavaScript

change that to:

JavaScript

Yes, it is that simple! You were very close, but just needed a little help spotting the bug. 😉

You can run the snippet below to see your code working.

Addendum: There’s one other (optional) change you might want to make. Append a “removeAttr” to the following line. This will clear the previous selection when a different department is selected.

JavaScript

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement