Making a generic jQuery "addListener"
<p>I'm trying to reproduce Google Maps' <code>addListener</code> except with jQuery listeners for clicks, keypresses, etc.</p>

<p>In Moogle Maps, you can write <code>.event.addListener(map, 'click', function()...</code> or replace 'click' with 'drag', 'mouseover', etc. For jQuery, I see a parallel with 'click', 'keypress', etc.</p>

<p>I'm stuck at the point where you could maybe pass the trigger type as a variable. It was straightforward to declare individual wrapper listeners, like </p>

<pre><code>  function addClickListener(id, fn) {
    $(id).click(function() { fn(event); });
  }      

  // and

  function addKeyPressListener(id, fn) {
    $(id).keypress(function() { fn(event); });
  }
</code></pre>

<p>What I am looking for is broken, shown below. I can't figure out how Google passes a String like 'click' and make that call a function. Maybe with <code>eval</code>? <em>shudder</em></p>

<pre><code>  function addListener(id, type, fn) {
    typeFn = $.type;
    $(id).typeFn(function() { fn(event); });
  }
</code></pre>

<p>Finally: <a href="http://jsfiddle.net/YCVVL/" rel="nofollow">http://jsfiddle.net/YCVVL/</a></p>
