EventSelectors.start(Rules)

I’ve been using Behaviours (listed on the Prototype website) since it came out some time ago. Being able to separate out all the attr clutter from your tags (mouseover this, onclick that) made for enhanced readability and more organized js code. EventSelectors takes that up another notch, with better prototype integration (which means that EventSelectors itself is a tiny file), multiple selectors for a single function, and automatic reapplication.

Tonight was the first time I started using EventSelectors and I ran into a problem where I wasn’t getting back the element that was requested by the selection itself. Say I had this:


'div.section:click': function(element) {
....
}

With some html like so:


<div class="section" id="first">
<div class="inner">
Test
</div>
</div>
</code>

What was happening was that this particular rule kept coming back with the "inner" set as the returned element and not the element with id of "first" for which the selection should apply given the class of "section." Behaviour doesn't work like this at all as it always returns the element given by the selection. After some rummaging around off the event selector blog intro, I found this thread. Looks like a buglet and if you follow the instructions to remove or comment out that one line then it seems to work (look towards the bottom there).

I was surprised to find that after all this time (it’s been a while since this was introduced) that this buglet was still hiding. Even worse, I wonder if this might be considered a bug at all. Given the lack of response on the subject I can only guess that some people have accepted this as default behavior and are 1) working around it or 2) have developed their scripts utilizing this as correct. It does seem to me though that this might be “fixed” in a future version of the file and this is seemingly a big default behavior change.

Regardless, Behavioral separation is an important idea and EventSelectors is an excellent addition to any web developer’s standard library. Many thanks to Justin Palmer for his work.

Leave a Reply