$("#selUser").bind('change keyup', function() {
$('#selSubjects2').empty();
if($(this).val().length > 0)
$.each( $(this).val().split(','), function(i, val) {
var option = new Option(val, val);
option.selected = true;
console.log("option",option);
$('#selSubjects2').append(option);
});
$('#selSubjects2').trigger("change");
});
I am trying to remove the options from selsubjects2 using the empty method.
When selecting a new user from the SelUser dropdown, I want to clear all the options in selSubjects2 using the empty method.
Currently, after selecting permissions for User1, if I choose permissions for User2, it appends these new permissions to the existing options of User1. The goal is to entirely remove User1's permissions and then populate selSubjects2 with User2's permissions. This ensures that only the permissions of the currently selected user are displayed, removing the previous user's permissions entirely.

How do I get rid of user1's permissions completely and then append the user'2 permissions?

$(this).val()will be an array in a multi-select, not a comma-separated string.