search

Home  >  Q&A  >  body text

Combine two selectors with a jQuery object

I have two divs with ids: #addNew_tab and #sendCom_tab.

I want to click on any of them to trigger the same jQuery click() function.

I was thinking something like this:

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

But this doesn't work.

P粉122932466P粉122932466283 days ago331

reply all(2)I'll reply

  • P粉245489391

    P粉2454893912024-03-28 14:55:36

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

    reply
    0
  • P粉769413355

    P粉7694133552024-03-28 14:17:39

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

    Changed from:

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

    To:

    $("#addNew_tab, #sendCom_tab")

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

    It is equal to:

    $("#addNew_tab").add("#sendCom_tab")...

    reply
    0
  • Cancelreply