Trying to have jQuery access both the event.target and another element on click but receive the error:
Error: Syntax error, unrecognized expression: [object HTMLButtonElement],#element2
Selector code:
$(event.target+",#element2").closest("div").removeClass("alert-info").addClass("alert-success");
Any suggestions what I am doing wrong appreciated.
event.target
isn't a selector string, it's an element. You can wrap any element in a jQuery object:
$(event.target)
So you could perform your logic on both the element and the selector:
$(event.target).closest("div").removeClass("alert-info").addClass("alert-success");
$("#element2").closest("div").removeClass("alert-info").addClass("alert-success");
Or, two reduce a little repetition, you can use .add()
to combine jQuery objects into a set of objects:
$(event.target).add("#element2").closest("div").removeClass("alert-info").addClass("alert-success");