If I understand what you are trying to do (by looking at the code), this would be a better approach:
// If mute button is clicked place them into muted users list // check for duplicates in list $("#messages-wrapper").off('click', '.message button.muteButton'); $("#messages-wrapper").on('click', '.message button.muteButton', function(e) { $('#unMute').show(); //create userId reference variable var chatUserID = parseInt($(this).parent().parent().attr("data-type")); //store userId in muted user object mutedUsers[chatUserID] = {}; mutedUsers[chatUserID].id = chatUserID; mutedUsers[chatUserID].muted = true; }); this.isUserMuted = function isUserMuted(payload) { var mutedUser = ''; if (mutedUsers[payload.a] !== null) { mutedUser += ' muted'; } return mutedUser; };
The code saves the mutedUsers array, and the isUserMuted function checks to see if the user is provided in this array. In the code you provided, you attach a new event handler each time the isUserMuted function is isUserMuted ..
The isUserMuted function can even be shortened to:
this.isUserMuted = function isUserMuted(payload) { return mutedUsers[payload.a] !== null ? ' muted' : ''; };
source share