Hello, Can you populate a drop down list with results from a column in a table? I have a form where they need to fill in table with vendor names and then the form will go to a manager for approval, what I would like is then on the manager's form for them to pick a vendor from a drop list based on the vendors listed in table.
Question
Question
Answer
Here's one way to do it. This assumes your table has a Class Name of myTable, the field in the table with the vendor names has Class Name of myTableField, and the drop-down field has a Class Name of myDropDown:
$(document).ready(function() { //When the form is loaded, call the function to populate the existing table data into the dropdown loadTableDataToDropDown(); //When the table is changed, call the function to populate the table data into the dropdown $('.myTable').change(loadTableDataToDropDown); //This function is called to load all the values from the table into the drop-down //the options in the drop-down will be cleared, and the new options from the table //will be loaded. function loadTableDataToDropDown() { $('.myDropDown select').find('option').remove(); $('.myTableField input').each(function() { if ($(this).val() != '') { $('.myDropDown select').append(new Option($(this).val(), $(this).val(), false, false)); } }); } });
Hello,
This is really helpful with what I am trying to do. However, my Drop Down list is a part of another table (table 2) and if I use the existing code, only the first drop down (row 1) gets the pre-populated choices from previous table. Once I add another row to my table 2, my drop down is blank. Is there a change that I can make to this code to make sure all choices get added to every drop down for "n" number of rows in table 2?
Replies
This worked! Thank you
That's great! I'm glad it worked for you.
Please consider marking the question as answered.
Have a great day!