Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  173] [ 2]  / answers: 1 / hits: 116567  / 11 Years ago, tue, april 9, 2013, 12:00:00
function CascadeDropDowns(parentClass, childClass, action, callback) {
var DropDownId = $(parentClass + option:selected).val();

$.ajax({
url: /site/ + action,
data: { DropDownId: DropDownId },
dataType: json,
type: POST,
error: function () {
alert(An error occurred.);
},
success: function (data) {
var items = ;
$.each(data, function (i, item) {
items += <option value= + item.Value + > + item.Text + </option>;
});
$(childClass).html(items);
$(childClass)[0].selectedIndex = 0;
callback();
}
});
}

$(document).ready(function () {
// Populates all child drop downs on load
var callback = function () {
CascadeDropDowns(.ConfigGroupDDL, .ConfigNameDDL, GetParameters);
};

CascadeDropDowns(.DeviceTypeDDL, .ConfigGroupDDL, GetGroups, callback);

// Populates all child drop downs parent change
$(.DeviceTypeDDL).change(function () {
var callback = function () {
CascadeDropDowns(.ConfigGroupDDL, .ConfigNameDDL, GetParameters);
};
CascadeDropDowns(.DeviceTypeDDL, .ConfigGroupDDL, GetGroups, callback);
});
$(.ConfigGroupDDL).change(function () {
CascadeDropDowns(.ConfigGroupDDL, .ConfigNameDDL, GetParameters);
});
});


This runs fine and cascades the dropdowns in the right order, but firefox debugger shows an error and ie throws an alert and asks if Id liek to debug.



Any advice would be great


More From » javascript

 Answers
35

It is because you are not always passing the callback into that method.



success: function (data) {
var items = ;
$.each(data, function (i, item) {
items += <option value= + item.Value + > + item.Text + </option>;
});
$(childClass).html(items);
$(childClass)[0].selectedIndex = 0;
if(callback) callback(); //check before calling it.
}

[#79017] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacyl

Total Points: 131
Total Questions: 105
Total Answers: 94

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;