I working on a project and am limited to handling JavaScript events via the inline handlers like onclick
and onchange
. However, I need my handlers to be cross-browser. I'm used to using jQuery for this, but since I can't use $('#id').click(function() { ... })
I'm having some difficulty getting access to jQuery-style events.
For example, I want do this:
<table>
<tr onclick="if (event.target.tagName.toLowerCase() !== 'td') return false; ...">
<td>...</td>
<td>...</td>
<td><a>...</a></td>
<td><a>...</a></td>
</tr>
</table>
Basically, this code should only handle a clicks on the cells in the table that don't have any anchor sub-elements.
This works fine in Firefox and Chrome and pretty much any browser that uses W3C events. Unfortunately, in IE, it does not work.
What I'd like to do is something like:
if ($.Event(event).target.tagName.toLowerCase() !== 'td') return false; ...
For the onclick
handler, but it appears that when you wrap an event in jQuery in this way, it doesn't provide the target
field.
What am I doing wrong?