12

I have two divs with id's: #addNew_tab and #sendCom_tab.
I'd like clicking on either of these to trigger the same jQuery click() function.

I was thinking something like:

$("#addNew_tab", "#sendCom_tab").click(function(){
      //do stuff
});

but that doesn't work.

0

2 Answers 2

33
$("#addNew_tab, #sendCom_tab").click(function(){
      //do stuff
});

Changed from:

$("#addNew_tab", "#sendCom_tab")

To:

$("#addNew_tab, #sendCom_tab")

comma inside the selector("a, b") means the first plus the second; Just like with CSS selectors
(Well, it's a CSS selector...)

jQuery(selector)

Description: Accepts a string containing a CSS selector which is then used to match a set of elements.

It's equal to:

$("#addNew_tab").add("#sendCom_tab")...
Sign up to request clarification or add additional context in comments.

4 Comments

@ElliotBonneville it's the same way you would do with css without repeationg rules like a, div { display: none; }
@ElliotBonneville. It's just like add
cool, that did it. I knew it was something like that. Didn't think it could accept a comma in quotes but hey, learned something new. Thanks.
Oh, that's right, jQuery shares CSS handlers. Wow, that's quick the trick.
0
function doStuff() {
    // do stuff
}

$("#addNew_tab").click(doStuff);
$("#sendCom_tab").click(doStuff);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.