Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

click

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");
        });
like image 899
Angeline Avatar asked Sep 04 '09 06:09

Angeline


People also ask

Can you get multiple elements by ID?

The id must be unique. There can be only one element in the document with the given id . If there are multiple elements with the same id , then the behavior of methods that use it is unpredictable, e.g. document. getElementById may return any of such elements at random.

Can ID be used multiple times in HTML?

HTML id Attribute. The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.


1 Answers

Just use the comma separator in the selector:

$("#myFormsTabSubmitted, #formStatusSubmitted").click(function() {
  // do stuff
});
like image 117
cletus Avatar answered Nov 12 '22 06:11

cletus