Growing with the Web

Passing parameters to jQuery event handlers

Published
Tags:

Something everyone who works with jQuery should know, how to pass parameters to a jQuery event handler. Pass a data object as the first argument on the event, the contents of the object will be transferred onto the data variable of event.

<button id="one">button#one</button>
<button id="two">button#two</button>
$().ready(function () {
  $('button#one').click({ text: 'first button' }, handleClick);
  $('button#two').click({ text: 'second button' }, handleClick);
});

function handleClick(event) {
  alert('Clicked on the ' + event.data.text);
}

Like this article?
Subscribe for more!