How can I trigger this function if either “.ranking” or “.complaint select” are changed?
This change function adds and/or remove values from “increase_priority1” when the user selects a complaint and ranks the importance level of the issue. At the moment it’s only being triggered when “ranking” changes, and therefore if the user changes the issue but not the importance level it isn’t changing the value accordingly.
var $increase_priority1 = $(".increase_priority1");
// If value is "Shoulders", complaint was Shoulders too small (select name=Shoulders value=Too_small) and it was assigned a level 1 priority (select class="ranking shoulders" value=1).
// var's for other issues and priority levels go here
$(".ranking, .complaint select").change(function () {
var name = $(this).data("name"); //get priority name
if ($(".complaint select").val() === "Too_small" && $(this).val() == 1 && !$(this).data("increase_priority1")) {
//rank is 1, and not yet added to priority list
$("<option>", {
text: name,
val: name
}).appendTo($increase_priority1);
$(this).data("increase_priority1", true); //flag as a priority item
}
if ($(this).data("increase_priority1") && ($(this).val() != 1 || $(".complaint select").val() != "Too_small")) {
//is in priority list, but now demoted
$("option[value=" + name + "]", $increase_priority1).remove();
$(this).removeData("increase_priority1"); //no longer a priority item
}
// Similar If Statements to set & unset other elements go here
});
There are several complaint and ranking elements, reflecting the various areas customers complain about and ranking on a scale of 1 – 5.
Fiddle which shows this in context: http://jsfiddle.net/chayacooper/vWLEn/168/
It would probably be easier if you restructured your code. How about this:
HTML:
Now you can use the
<div>with classcomplaintto selectively target it’s content:JavaScript:
You should now be able to duplicate the ‘complaint div’ for other items (just make sure you change the IDs of the
<select>elements in the newdiv). There is an example here: http://jsfiddle.net/6YnQd/