dictionaries and dynamic dictionary keys
Hi guys, this question is more related to Javascript than to jQuery but I'm hoping one of you guys can point me in the right direction.
I'm trying to write a generic javascript function which will populate a secondary SELECT list based on an option selected in a primary SELECT list. The data for the secondary list is retrieved via an ajax getJSON call. This is how the function looks like this:
- function loadSecListFromPrimList(primListID, secListID, urlKeyID, loadingText, nonAvailText, ajaxURL) {
- var prim_list = $(primListID);
- var sec_list = $(secListID);
- if (prim_list.val() != 0) {
- sec_list.find('option').remove().end().append('<option selected value="0">' + loadingText + '</option>');
- $.getJSON(ajaxURL, {urlKeyID: prim_list.val()}, function(j){
- var options = '';
- for (var i = 0; i < j.length; i++) {
- options += '<option value="' + j[i][0] + '">' + j[i][1] + '</option>';
- }
- $('select' + secListID).html(options);
- })
- } else
- {
- sec_list.find('option').remove().end().append('<option selected value="0">' + nonAvailText + '</option>');
- }
- }
Notes: While the data is being loaded via ajax the secondary list text changes to something like: "busy loading...". Once the data is loaded it will clear and repopulate the secondary list.
And the HTML code to call this looks like this:
- <select name="prim_id" id="prim_id" onchange="loadSecListFromPrimList('#prim_id', '#sec_id', 'cid', 'Loading Secondary Data...', 'No Secondary Data Available', '/ajax.php')">
- <option value="0">No Option Selected</option>
- <option value="1">Option 1</option>
- <option value="2">Option 2</option>
- </select>
The only thing I'm stuck with is to be able to pass the "key" to be used in the dictionary for the data of the getJSON call:
- $.getJSON(ajaxURL, {urlKeyID: prim_list.val()}, function(j)
The urlKeyID variable is passed through as a parameter of the loadSecondaryListFromList() function and needs to be dynamic so I can use the function all over my website. So I finally get to my question:
How do I use a value that was passed through a parameter of a function as a key in a dictionary? In this case I'm trying to set 'cid' as the key name (as per my HTML). I know it is a stupid question but I am baffled. I hope my explanation is detailed enough.