Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - attach .click() to multiple variables

Tags:

jquery

click

I have the following code:

var buttonOne = $("#buttonOne");
var buttonTwo = $("#buttonTwo");

// opens an already initialized modal window 
(buttonOne, buttonTwo).click(function()
{
    modalBoxContainer.dialog("open");
});

This is not working. It will work if I use the elements, but not with the variables.

Any ideas?

like image 455
IntricatePixels Avatar asked Nov 20 '25 18:11

IntricatePixels


2 Answers

Use the .add method.

buttonOne.add(buttonTwo).click(...);
like image 159
Kevin B Avatar answered Nov 23 '25 09:11

Kevin B


Try combining the selector?

$("#buttonOne, #buttonTwo").click(...

Although you should really use on (jQuery 1.7) or delegate for pre-1.7, this will create the one event for all elements in the handler. Something like:

$(document).on("click", "#buttonOne, #buttonTwo", function () {
    //function here
});

on documentation: http://api.jquery.com/on/

delegate documentation: http://api.jquery.com/delegate/

like image 44
mattytommo Avatar answered Nov 23 '25 11:11

mattytommo