Can I code same functionality for 2 id element's in one click function?

click, jquery

Solution

Just use the comma separator in the selector:

$("#myFormsTabSubmitted, #formStatusSubmitted").click(function() {
  // do stuff
});

Problem

Is there any method to have the same coding for the click function of two different id elements in one click function? For example,I have two link elements with ids, myFormsTabSubmitted and formStatusSubmitted. Both the link is of same functionality. When I click both these links, the same div element("submitted") is showed,and other divs are hidden. So instead of writing two click functions, one for myFormsTabSubmitted and another for formStatusSubmitted, can I have one click function for both? Like ``` $('#myFormsTabSubmitted #formStatusSubmitted').click(function(){ $('#allMyForms').hide(); $('#draftedMyForms').hide(); $('#submittedMyForms').show(); $('#myFormsTab').find(".selected").removeClass(); $('#myFormsTabSubmitted').addClass("selected"); }); ```

Original source