I often need to register a single-purpose event listener to multiple HTML elements (e.g. change
, click
), in a loop.
I need a way to differentiate between the target elements when the event fires. Ideally the listener should have a variable that changes depending on which element fires the event.
A straight-forward solution would be to set the id
attribute in the loop (jsfiddle), and refer to this.id
within the handler. Or if additional fields are required, the data-*
attributes, and instead accessing this.dataset.id
.
However these end up adding to the actual markup of the page, and can only store strings. If you have a lot of data to assign, of specific types other than string, it can be impractical, and require unnecessary type checking, or integer parsing.
I got around this by defining a new property called param
on each HTML element, and I could access it in the handler like so: this.param.address
. But that turns out to be a bad idea - a 'feature' not to be relied on due to browser consistencies and a lack it being in any standard.
Another way is supplying arguments to the listener through a closure (jsfiddle). The downside of this method is that it returns a unique listener for each element, which might affect performance versus just a single handler.
Yet another method I had thought of was storing the actual element references with data in an array, and performing a lookup of this
from the event handler (JSFiddle).
So my question is: Is there a better (and correct/future-proof) way to pass/assign data to element event handlers, without having to store it as an attribute string?
P.S. I don't want to have to rely on jQuery for this either.
With jQuery it's possible to use $(el).bind("click", {foo:i}, buttonClicked)
and $.data(el, "param", {foo:i})
to accomplish the task. However it seems to rely on being able to set a custom property to the element's object, much what I proposed for using param
as a custom property for my purposes. Does that mean it's fine as long as the property name is unique enough?...