JacobTheDev
JacobTheDev

Reputation: 18520

Fire Function if Button or Other Button Is Clicked

This is probably really obvious, but because it's hard to clearly describe, I can't find anything through searching.

I want to do something where if a user clicks on any of several buttons, one event gets triggered. Is this possible? This is what I've tried, but it doesn't appear to work:

$("#button1" || "#button2").click(function() {alert("example")});

Upvotes: 1

Views: 277

Answers (2)

user1106925
user1106925

Reputation:

Just apply the handler to all the buttons.

$("#button1,#button2").click(...

Using the , creates a multiple selector, and because of jQuery's "implicit iteration", it will add the click() handler to all matched elements.


That said, if there are many buttons, usually it's nicer to give all the buttons a common class.

$(".my_buttons").click(...

Upvotes: 3

Kevin Boucher
Kevin Boucher

Reputation: 16675

Try this:

$("#button1, #button2").click(function() {alert("example")});

http://api.jquery.com/multiple-selector/

Upvotes: 4

Related Questions