<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Need Help With Table Row Events</title>
	<atom:link href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/feed/" rel="self" type="application/rss+xml" />
	<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/</link>
	<description>Things that Eric A. Meyer, CSS expert, writes about on his personal Web site; it&#039;s largely Web standards and Web technology, but also various bits of culture, politics, personal observations, and other miscellaneous stuff</description>
	<lastBuildDate>Wed, 17 Mar 2010 18:47:03 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Borgar.net &#187; Node members or Node attributes</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-436813</link>
		<dc:creator>Borgar.net &#187; Node members or Node attributes</dc:creator>
		<pubDate>Sat, 17 Jan 2009 01:18:39 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-436813</guid>
		<description>[...] given it a bit of thought I think that this problem stems from a slight confusion as to how DOM nodes&#8217; attributes [...]</description>
		<content:encoded><![CDATA[<p>[...] given it a bit of thought I think that this problem stems from a slight confusion as to how DOM nodes&#8217; attributes [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rahul</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-396282</link>
		<dc:creator>Rahul</dc:creator>
		<pubDate>Tue, 29 Jul 2008 14:01:40 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-396282</guid>
		<description>Hello Eric,

Thanks for posting this question. I was facing the same problem. Your post and the comment did help me out. Thanks. 

But going further, I am facing same problem as mentioned in comment #8. i.e &quot;The only problem is that the id and event have scoping issues and will be bound to the last values they encountered as we set up all the onclicks...&quot;

You certainly must have faced same problem. It would be great if you post how you work around that issue as well. 

Thanks in advance..

Rahul</description>
		<content:encoded><![CDATA[<p>Hello Eric,</p>
<p>Thanks for posting this question. I was facing the same problem. Your post and the comment did help me out. Thanks. </p>
<p>But going further, I am facing same problem as mentioned in comment #8. i.e &#8220;The only problem is that the id and event have scoping issues and will be bound to the last values they encountered as we set up all the onclicks&#8230;&#8221;</p>
<p>You certainly must have faced same problem. It would be great if you post how you work around that issue as well. </p>
<p>Thanks in advance..</p>
<p>Rahul</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Gregory</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-382641</link>
		<dc:creator>Andrew Gregory</dc:creator>
		<pubDate>Thu, 05 Jun 2008 03:55:47 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-382641</guid>
		<description>Eric, if you can, please, please, please change the third parameter of addEventListener in your comment #37 to false! Having true there will set up what&#039;s called a &quot;capturing&quot; event handler (explained on PPKs page) which is NOT what you want, while the IE compatible attachEvent you&#039;ve got on the next line implements non-capturing.

Unintended event capturing breaks pages for the minor browsers that support it (currently Opera and Safari, but soon Firefox 3).</description>
		<content:encoded><![CDATA[<p>Eric, if you can, please, please, please change the third parameter of addEventListener in your comment #37 to false! Having true there will set up what&#8217;s called a &#8220;capturing&#8221; event handler (explained on PPKs page) which is NOT what you want, while the IE compatible attachEvent you&#8217;ve got on the next line implements non-capturing.</p>
<p>Unintended event capturing breaks pages for the minor browsers that support it (currently Opera and Safari, but soon Firefox 3).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381427</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Sun, 01 Jun 2008 04:00:44 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381427</guid>
		<description>I dunno, &lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381422&quot; rel=&quot;nofollow&quot;&gt;Jason&lt;/a&gt;, but I&#039;m just now most of the way done with a detailed rant on that very topic.  I expect to post it Sunday or Monday (probably the latter).</description>
		<content:encoded><![CDATA[<p>I dunno, <a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381422" rel="nofollow">Jason</a>, but I&#8217;m just now most of the way done with a detailed rant on that very topic.  I expect to post it Sunday or Monday (probably the latter).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Penney</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381422</link>
		<dc:creator>Jason Penney</dc:creator>
		<pubDate>Sun, 01 Jun 2008 03:29:31 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381422</guid>
		<description>I totally agree on the XHTML2/href thing. I remeber thinking how much that would simplify things. Was there a reason that was dropped or was it just overlooked?</description>
		<content:encoded><![CDATA[<p>I totally agree on the XHTML2/href thing. I remeber thinking how much that would simplify things. Was there a reason that was dropped or was it just overlooked?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381409</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Sun, 01 Jun 2008 00:53:34 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381409</guid>
		<description>&lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381356&quot; rel=&quot;nofollow&quot;&gt;Remy&lt;/a&gt;: if by the &quot;delegation code&quot; you mean the actual function &lt;code&gt;gotoCall&lt;/code&gt;, then that&#039;s what I meant.  Something like:

&lt;pre&gt;&lt;code&gt;function callGoto(e) {
  if (!e) e = event;
  var src = e.target &#124;&#124; e.srcElement;
  while (src.nodeName != &quot;TABLE&quot;) {
    if (src.nodeName == &quot;TR&quot;) {
      if (src.class == &quot;one&quot; &#124;&#124; src.class == &quot;two&quot; &#124;&#124; src.id != &quot;blah&quot;) {
        var id = src.id;
        return goto(id, conferenceID);
      }
    }
    src = src.parentNode;
  }
}
&lt;/code&gt;&lt;/pre&gt;

...though there&#039;s probably a less clumsy way to roll all the innermost &quot;if&quot;s together, and that innermost &quot;if&quot; is probably pseudocode as it stands (I don&#039;t think &lt;code&gt;el.class&lt;/code&gt; is really valid, is it?).  But that&#039;s basically what I was meaning.  Good to know that brute-forcing a click event onto every row of tables this size isn&#039;t going to be a big performance loss.

Of course, absolutely none of this would be necessary if HTML5 restored the &lt;code&gt;href&lt;/code&gt; attribute to all elements, which was one of the few things I thought XHTML2 got absolutely totally 110% correct.  Then all I&#039;d have to do is slap the necessary &lt;code&gt;href&lt;/code&gt; on each &lt;code&gt;tr&lt;/code&gt; element and be finished with this really stupidly simple requirement in twenty seconds instead of juggling a dozen script eggs in an attempt to make stuff clickable.</description>
		<content:encoded><![CDATA[<p><a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381356" rel="nofollow">Remy</a>: if by the &#8220;delegation code&#8221; you mean the actual function <code>gotoCall</code>, then that&#8217;s what I meant.  Something like:</p>
<pre><code>function callGoto(e) {
  if (!e) e = event;
  var src = e.target || e.srcElement;
  while (src.nodeName != "TABLE") {
    if (src.nodeName == "TR") {
      if (src.class == "one" || src.class == "two" || src.id != "blah") {
        var id = src.id;
        return goto(id, conferenceID);
      }
    }
    src = src.parentNode;
  }
}
</code></pre>
<p>&#8230;though there&#8217;s probably a less clumsy way to roll all the innermost &#8220;if&#8221;s together, and that innermost &#8220;if&#8221; is probably pseudocode as it stands (I don&#8217;t think <code>el.class</code> is really valid, is it?).  But that&#8217;s basically what I was meaning.  Good to know that brute-forcing a click event onto every row of tables this size isn&#8217;t going to be a big performance loss.</p>
<p>Of course, absolutely none of this would be necessary if HTML5 restored the <code>href</code> attribute to all elements, which was one of the few things I thought XHTML2 got absolutely totally 110% correct.  Then all I&#8217;d have to do is slap the necessary <code>href</code> on each <code>tr</code> element and be finished with this really stupidly simple requirement in twenty seconds instead of juggling a dozen script eggs in an attempt to make stuff clickable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remy Sharp</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381356</link>
		<dc:creator>Remy Sharp</dc:creator>
		<pubDate>Sat, 31 May 2008 18:15:49 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381356</guid>
		<description>By way of pushing best practises a little further, add this code to your JS snippets:

&lt;code&gt;addEvent = (function () {
&#160;&#160;return document.body.addEventListener ? function (e, ev, fn) {
&#160;&#160;&#160;&#160;e.addEventListener(ev, fn, false);
&#160;&#160;} : function () {
&#160;&#160;&#160;&#160;e.attachEvent(&quot;on&quot; + ev, fn);
&#160;&#160;};
})();

// usage:
addEvent(element, &#039;click&#039;, function () {
&#160;&#160;// do stuff
});&lt;/code&gt;

It&#039;s a pretty common design pattern - it looks for the method to attach the events and creates the appropriate function.

Re: delegation to solve your problem.  The way you&#039;ve suggested, i.e. checking for a class to ignore particular rows would be the best way to do it  (until we get data attributes in HTML5...) - but it wouldn&#039;t be in the gotoCall function - it would be in the delegation code.  

However, for the size of the table - there&#039;s definitely isn&#039;t going to be a measurable performance benefit.  Go with what makes more sense to you now :-)</description>
		<content:encoded><![CDATA[<p>By way of pushing best practises a little further, add this code to your JS snippets:</p>
<p><code>addEvent = (function () {<br />
&nbsp;&nbsp;return document.body.addEventListener ? function (e, ev, fn) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.addEventListener(ev, fn, false);<br />
&nbsp;&nbsp;} : function () {<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.attachEvent("on" + ev, fn);<br />
&nbsp;&nbsp;};<br />
})();</p>
<p>// usage:<br />
addEvent(element, 'click', function () {<br />
&nbsp;&nbsp;// do stuff<br />
});</code></p>
<p>It&#8217;s a pretty common design pattern &#8211; it looks for the method to attach the events and creates the appropriate function.</p>
<p>Re: delegation to solve your problem.  The way you&#8217;ve suggested, i.e. checking for a class to ignore particular rows would be the best way to do it  (until we get data attributes in HTML5&#8230;) &#8211; but it wouldn&#8217;t be in the gotoCall function &#8211; it would be in the delegation code.  </p>
<p>However, for the size of the table &#8211; there&#8217;s definitely isn&#8217;t going to be a measurable performance benefit.  Go with what makes more sense to you now :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Larsen</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381355</link>
		<dc:creator>John Larsen</dc:creator>
		<pubDate>Sat, 31 May 2008 17:56:33 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381355</guid>
		<description>How about iterating over the rows and setting a property to flag whether the row should be ignored? Then use delegation with an initial test of the flag. If the flag is false just return.</description>
		<content:encoded><![CDATA[<p>How about iterating over the rows and setting a property to flag whether the row should be ignored? Then use delegation with an initial test of the flag. If the flag is false just return.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381080</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Sat, 31 May 2008 01:31:04 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381080</guid>
		<description>Um, you just posted more or less what ProggerPete did, except not in actual JS, and nothing about the parts that I said elude me.  I do appreciate the effort, but unfortunately it seems to have gone to repeating an existing answer and not answering the existing questions.

&lt;strong&gt;Update:&lt;/strong&gt; &lt;a href=&quot;http://www.quirksmode.org/js/events_advanced.html&quot; rel=&quot;nofollow&quot;&gt;thanks to PPK&lt;/a&gt; (who else?), I worked out how to add the table handler in a cross-browser fashion:

&lt;pre&gt;
if (t.addEventListener) {t.addEventListener(&#039;click&#039;,callGoto,true);}
if (t.attachEvent) {t.attachEvent(&#039;onclick&#039;,callGoto);}
&lt;/pre&gt;

Not the most robust, perhaps, but it works well enough.  I also figured out how to handle the event determination in a way that made sense to me, which was to set up a global variable and give it the needed value in my onLoad routine.

It was at that point I realized that event delegation probably won&#039;t work well in my case, because not every row of the table should be clickable---in my current working file, about three out of ~35 rows are non-clickable.  So it actually makes more sense for me to collect all the rows I want to make clickable and assign them their own event handlers, I think.  The only way around it I can see is to do delegation and then have more code in &#039;gotoCall&#039; that checks against a class or a list of IDs that should be ignored, and that seems kind of ugly.  This table isn&#039;t ever going to have more than 60 or 70 rows, and that&#039;s a very padded estimate.

But I did learn a lot more about events and event handling, for which I thank you all!</description>
		<content:encoded><![CDATA[<p>Um, you just posted more or less what ProggerPete did, except not in actual JS, and nothing about the parts that I said elude me.  I do appreciate the effort, but unfortunately it seems to have gone to repeating an existing answer and not answering the existing questions.</p>
<p><strong>Update:</strong> <a href="http://www.quirksmode.org/js/events_advanced.html" rel="nofollow">thanks to PPK</a> (who else?), I worked out how to add the table handler in a cross-browser fashion:</p>
<pre>
if (t.addEventListener) {t.addEventListener('click',callGoto,true);}
if (t.attachEvent) {t.attachEvent('onclick',callGoto);}
</pre>
<p>Not the most robust, perhaps, but it works well enough.  I also figured out how to handle the event determination in a way that made sense to me, which was to set up a global variable and give it the needed value in my onLoad routine.</p>
<p>It was at that point I realized that event delegation probably won&#8217;t work well in my case, because not every row of the table should be clickable&#8212;in my current working file, about three out of ~35 rows are non-clickable.  So it actually makes more sense for me to collect all the rows I want to make clickable and assign them their own event handlers, I think.  The only way around it I can see is to do delegation and then have more code in &#8216;gotoCall&#8217; that checks against a class or a list of IDs that should be ignored, and that seems kind of ugly.  This table isn&#8217;t ever going to have more than 60 or 70 rows, and that&#8217;s a very padded estimate.</p>
<p>But I did learn a lot more about events and event handling, for which I thank you all!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tuan</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-381074</link>
		<dc:creator>Tuan</dc:creator>
		<pubDate>Sat, 31 May 2008 01:16:59 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-381074</guid>
		<description>here&#039;s some pseudo code for you. Only thing you have to worry about is getting the event target from the event object. The rest is pretty simple. If you&#039;re using a library like Prototype, you could do all this with just a few lines.

var handleClick = function(event) {
  // get the element inside the table that fired the event
  var el = event.target; // add logic here for cross browser

  // check if the element is a TR, if not then keep going up the
  // DOM tree til you reach one and stop when you&#039;re at the table
  while(el.tagName != &#039;TR&#039;) {
    el = el.parentNode;
    if(el.tagName == &#039;TABLE&#039;) { // reached the table itself, passed any TR element, bail out
      return;
    }
  }

  // if you&#039;re here, you got the TR and assuming it has an id, just call your function
  goTo(el.id, event);
}

....</description>
		<content:encoded><![CDATA[<p>here&#8217;s some pseudo code for you. Only thing you have to worry about is getting the event target from the event object. The rest is pretty simple. If you&#8217;re using a library like Prototype, you could do all this with just a few lines.</p>
<p>var handleClick = function(event) {<br />
  // get the element inside the table that fired the event<br />
  var el = event.target; // add logic here for cross browser</p>
<p>  // check if the element is a TR, if not then keep going up the<br />
  // DOM tree til you reach one and stop when you&#8217;re at the table<br />
  while(el.tagName != &#8216;TR&#8217;) {<br />
    el = el.parentNode;<br />
    if(el.tagName == &#8216;TABLE&#8217;) { // reached the table itself, passed any TR element, bail out<br />
      return;<br />
    }<br />
  }</p>
<p>  // if you&#8217;re here, you got the TR and assuming it has an id, just call your function<br />
  goTo(el.id, event);<br />
}</p>
<p>&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380981</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Fri, 30 May 2008 20:17:39 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-380981</guid>
		<description>Unfortunately, while I get the principles behind event delegation, I don&#039;t know how to add a click handler to the table such that it will take advantage of &lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380775&quot; rel=&quot;nofollow&quot;&gt;the routine ProggerPete posted&lt;/a&gt;.  The actual routine I understand.  Just not how to properly invoke it.

Oh wait, no, I don&#039;t understand one part of it, which is the &#039;idEventMap&#039; part.  Sorry.

Oh, or how to do it so it works in IE7, which it doesn&#039;t when I try it in such a way that it works in Gecko.  Sorry again.</description>
		<content:encoded><![CDATA[<p>Unfortunately, while I get the principles behind event delegation, I don&#8217;t know how to add a click handler to the table such that it will take advantage of <a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380775" rel="nofollow">the routine ProggerPete posted</a>.  The actual routine I understand.  Just not how to properly invoke it.</p>
<p>Oh wait, no, I don&#8217;t understand one part of it, which is the &#8216;idEventMap&#8217; part.  Sorry.</p>
<p>Oh, or how to do it so it works in IE7, which it doesn&#8217;t when I try it in such a way that it works in Gecko.  Sorry again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tuan</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380967</link>
		<dc:creator>Tuan</dc:creator>
		<pubDate>Fri, 30 May 2008 19:32:56 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-380967</guid>
		<description>oh man, it took 30 posts for someone to suggest event delegation. It should have been the first!

facepalm</description>
		<content:encoded><![CDATA[<p>oh man, it took 30 posts for someone to suggest event delegation. It should have been the first!</p>
<p>facepalm</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Meyer</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380921</link>
		<dc:creator>Eric Meyer</dc:creator>
		<pubDate>Fri, 30 May 2008 16:48:51 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-380921</guid>
		<description>Thanks for that, &lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380863&quot; rel=&quot;nofollow&quot;&gt;Eric&lt;/a&gt;-- I had no idea you could set up multiple variables in the first term of a &#039;for&#039; loop&#039;s parameters!  Makes sense that it would be a performance win, and I always like to learn best practices.  I&#039;m adding that as soon as I post this comment.

&lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380562&quot; rel=&quot;nofollow&quot;&gt;Jeremy&lt;/a&gt;, I&#039;ve no doubt I&#039;m coding old-school, and I did try some of the functionality-testing code you suggest.  It wasn&#039;t helping (due to my botching the whole &#039;onclick&#039; thing) so I took it out.  Now that I&#039;ve got things actually working thanks to all you awesome folks, I&#039;ll start adding that kind of feature-detected code back in.  I&#039;ll also look into delegation and bubbling as suggested by &lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380775&quot; rel=&quot;nofollow&quot;&gt;ProggerPete&lt;/a&gt; and &lt;a href=&quot;http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380618&quot; rel=&quot;nofollow&quot;&gt;Justin&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>Thanks for that, <a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380863" rel="nofollow">Eric</a>&#8211; I had no idea you could set up multiple variables in the first term of a &#8216;for&#8217; loop&#8217;s parameters!  Makes sense that it would be a performance win, and I always like to learn best practices.  I&#8217;m adding that as soon as I post this comment.</p>
<p><a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380562" rel="nofollow">Jeremy</a>, I&#8217;ve no doubt I&#8217;m coding old-school, and I did try some of the functionality-testing code you suggest.  It wasn&#8217;t helping (due to my botching the whole &#8216;onclick&#8217; thing) so I took it out.  Now that I&#8217;ve got things actually working thanks to all you awesome folks, I&#8217;ll start adding that kind of feature-detected code back in.  I&#8217;ll also look into delegation and bubbling as suggested by <a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380775" rel="nofollow">ProggerPete</a> and <a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380618" rel="nofollow">Justin</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric DeLabar</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380863</link>
		<dc:creator>Eric DeLabar</dc:creator>
		<pubDate>Fri, 30 May 2008 12:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-380863</guid>
		<description>Glad you found your solution, but here&#039;s a little constructive criticism on performance.  In general, with JavaScript, when using a for loop it&#039;s better to set a variable for the length than to call the length property on every iteration through the loop.  

e.g.&lt;code&gt;
...
var size = mapStates.length;
for (x = 0; x &lt; size; x++) {
...
&lt;/code&gt;

or for brevity&#039;s sake:
&lt;code&gt;
...
for (x = 0, size = mapStates.length; x &lt; size; x++) {
...
&lt;/code&gt;

In JavaScript the value of length could change for each iteration, so it&#039;s evaluated each time through instead of just being checked like a variable would be.  With all of the JavaScript being written for today&#039;s &quot;Web 2.0&quot; sites every millisecond counts, and since this is a learning exercise it&#039;s better to learn the best practice!</description>
		<content:encoded><![CDATA[<p>Glad you found your solution, but here&#8217;s a little constructive criticism on performance.  In general, with JavaScript, when using a for loop it&#8217;s better to set a variable for the length than to call the length property on every iteration through the loop.  </p>
<p>e.g.<code><br />
...<br />
var size = mapStates.length;<br />
for (x = 0; x &lt; size; x++) {<br />
...<br />
</code></p>
<p>or for brevity&#8217;s sake:<br />
<code><br />
...<br />
for (x = 0, size = mapStates.length; x &lt; size; x++) {<br />
...<br />
</code></p>
<p>In JavaScript the value of length could change for each iteration, so it&#8217;s evaluated each time through instead of just being checked like a variable would be.  With all of the JavaScript being written for today&#8217;s &#8220;Web 2.0&#8243; sites every millisecond counts, and since this is a learning exercise it&#8217;s better to learn the best practice!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Bex</title>
		<link>http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comment-380776</link>
		<dc:creator>Peter Bex</dc:creator>
		<pubDate>Fri, 30 May 2008 08:36:17 +0000</pubDate>
		<guid isPermaLink="false">http://meyerweb.com/?p=906#comment-380776</guid>
		<description>In common parlance, &quot;method&quot; just means a function on an object, but the difference between function and method is not made in Javascript. You can assign any function to any property on an object and it &quot;becomes&quot; a method.

One real difference you can notice in Javascript is that when the function is called as a method (ie, like &lt;code&gt;obj.propertyname(arg1, arg2);&lt;/code&gt;), there is a special variable called &lt;code&gt;this&lt;/code&gt; that has the value of the object that the method was called on (&lt;code&gt;obj&lt;/code&gt; in the example). If a function is called directly (not &quot;on an object&quot;), &lt;code&gt;this&lt;/code&gt; has the value of the &quot;global object&quot;, which is &lt;code&gt;window&lt;/code&gt; in Javascript.</description>
		<content:encoded><![CDATA[<p>In common parlance, &#8220;method&#8221; just means a function on an object, but the difference between function and method is not made in Javascript. You can assign any function to any property on an object and it &#8220;becomes&#8221; a method.</p>
<p>One real difference you can notice in Javascript is that when the function is called as a method (ie, like <code>obj.propertyname(arg1, arg2);</code>), there is a special variable called <code>this</code> that has the value of the object that the method was called on (<code>obj</code> in the example). If a function is called directly (not &#8220;on an object&#8221;), <code>this</code> has the value of the &#8220;global object&#8221;, which is <code>window</code> in Javascript.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head profile="http://gmpg.org/xfn/1">
<title>meyerweb.com</title>
<link rel="openid.server" href="http://www.myopenid.com/server">
<link rel="openid.delegate" href="http://emeyer.myopenid.com/">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link rel="shortcut icon" href="/favicon.ico"><link rel="home" href="http://meyerweb.com/" title="Home" ><link rel="stylesheet" href="http://meyerweb.com/ui/meyerweb.css" type="text/css" media="screen, projection"><link rel="stylesheet" href="http://meyerweb.com/ui/theme.css" type="text/css" media="screen, projection" id="themeLink"><link rel="stylesheet" href="http://meyerweb.com/ui/print.css" type="text/css" media="print"><script src="http://meyerweb.com/ui/addresses.js" type="text/javascript"></script><link rel="stylesheet" href="/ui/wordpress.css" type="text/css" media="screen">
<link rel="stylesheet" href="/ui/tfe.css" type="text/css" media="screen">
<link rel="stylesheet" href="/ui/home.css" type="text/css" media="screen">
<link rel="alternate" type="application/rss+xml" title="Thoughts From Eric" href="/eric/thoughts/rss2/full" />
<link rel="alternate" type="application/rss+xml" title="Thoughts From Eric (only technical posts)" href="/eric/thoughts/category/tech/rss2/full" />
<link rel="alternate" type="application/rss+xml" title="Thoughts From Eric (only personal posts)" href="/eric/thoughts/category/personal/rss2/full" />
<link rel="alternate" type="application/rss+xml" title="Distractions" href="/eric/thoughts/recent-links/rss2" />
<link rel="alternate" type="application/rss+xml" title="Excuse of the Day" href="/feeds/excuse/rss20.xml" />
</head>
<body id="www-meyerweb-com" class="hpg">

<div id="sitemast"><h1><a href="/"><span>meyerweb</span>.com</a></h1></div><div id="search"><h4>Exploration</h4><!-- SiteSearch Google --><form method="get" action="http://www.google.com/custom" target="_top"><div><input type="hidden" name="domains" value="meyerweb.com"></input><label for="sbb" style="display: none">Submit search form</label><input type="submit" name="sa" value="Google Search" id="sbb"></input><label for="sbi" style="display: none">Enter your search terms</label><input type="text" name="q" size="31" maxlength="255" value="" id="sbi"></input><p><input type="radio" name="sitesearch" value="meyerweb.com" checked id="ss1"></input><label for="ss1" title="Search meyerweb.com">meyerweb.com</label><input type="radio" name="sitesearch" value="" id="ss0"></input><label for="ss0" title="Search the Web">Web</label></p><input type="hidden" name="client" value="pub-3772084027748653"></input><input type="hidden" name="forid" value="1"></input><input type="hidden" name="ie" value="ISO-8859-1"></input><input type="hidden" name="oe" value="ISO-8859-1"></input><input type="hidden" name="safe" value="active"></input><input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1"></input><input type="hidden" name="hl" value="en"></input></div></form><!-- SiteSearch Google --><!-- <form method="get" action="http://www.google.com/custom"><div><input type="submit" name="sa" value="Search"><input type="text" name="q" size="20" maxlength="255" value=""><input type="hidden" name="sitesearch" value="meyerweb.com"></div></form><small><a href="http://www.google.com/search">Powered by Google</a></small> --></div><div id="main"><div class="skipper">Skip to: <a href="#extra">site navigation/presentation</a></div><div class="skipper">Skip to: <a href="#thoughts">Thoughts From Eric</a></div>
<div id="thoughts">


<div class="entry">
<h3><a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/" rel="bookmark" title="Permanent Link: Need Help With Table Row Events">Need Help With Table Row Events</a></h3>
<ul class="meta">
<li class="date">Thu 29 May 2008</li>
<li class="time">1706</li>
<li class="cat"><a href="http://meyerweb.com/eric/thoughts/category/tech/browsers/" title="View all posts in Browsers" rel="category tag">Browsers</a><br> <a href="http://meyerweb.com/eric/thoughts/category/tech/javascript/" title="View all posts in JavaScript" rel="category tag">JavaScript</a></li>
<li class="cmt"><a href="http://meyerweb.com/eric/thoughts/2008/05/29/need-help-with-table-row-events/#comments">45 responses</a></li>
<li></li><li></li></ul>

<div class="text">
<p>
Here&#8217;s a late-week call for assistance in the JavaScript realm, specifically in making IE do what I need and can make happen in other browsers.  I&#8217;d call this a LazyWeb request except I&#8217;ve been trying to figure out how to do it all <em>[censored]</em> afternoon, and it doesn&#8217;t <em>[censored]</em> work no matter how many <em>[censored]</em> semi-related examples I find online that work just <em>[censored]</em> fine, but still don&#8217;t <em>[censored]</em> help me <em>[censored]</em> fix this <em>[censored]</em> problem.  <em>[doubly censored]</em>!
</p>
<p>
I have a table.  (Yes, for data.)  In the table are rows, of course, and each row has a number of cells.  I want to walk through the rows and dynamically add an &#8216;onclick&#8217; event to every row.  The actual event is slightly different for each row, but in every case, it&#8217;s supposed to call a function and pass some parameters (which are the things that change).  Here&#8217;s how I&#8217;m doing it:
</p>
<pre>
var event = '5'; // in the real code this is passed into the surrounding function
var mapStates = getElementsByClassName('map','tr');
for (x = 0; x < mapStates.length; x++) {
	var el = mapStates[x];
	var id = el.getAttribute('id');
	var val = "goto('" + id + "','" + event + "');";
	el.setAttribute('onclick',val);
}
</pre>
<p>
Okay, so that works fine in Gecko.  It doesn't work at all in IE.  I changed <code>el.setAttribute('onclick',val);
</code> to <code>el.onclick = val;</code> per some advice I found online and that completely failed in everything.  Firebug told me "el.onclick is not a function".  Explorer just silently did nothing, like always.
</p>
<p>
So how am I supposed to make this work in IE, let alone in IE and Gecko-based and WebKit-based and all other modern browsers?
</p>
<p>
Oh, and do <strong>not</strong> tell me that framework X or library Q does this so easily, because I'm trying to <em>learn</em> here, not have someone else's code hand-wave the problem away.  Pointing me directly to the actual code block inside a framework or library that makes this sort of thing possible:  that's totally fine.  I may not understand it, but at least there will be JS for me to study and ask questions about.  Ditto for pointing me to online examples of doing the exact same thing, which I tried to find in Google but could not: much appreciated.
</p>
<p>
Help, please?
</p>
<p><strong>Update:</strong> many, many commenters helped me see what I was missing and therefore doing wrong---thank you all!  For those wondering what I was wondering, check out the comments.  There are a lot of good examples and quick explanations there.</p></pre></div>

</div>

</div>
<p style="font-size: 90%; text-align: right; margin-top: 0.5em; padding-top: 0;">(If you care, there's even an <a href="/eric/thoughts/page/2/">archive of previous thoughts</a>...)</p>

</div><div id="extra"><div class="panel" id="archipelago"><h4>Identity Archipelago</h4><ul><li><a href="http://flickr.com/photos/meyerweb/" rel="me">Flickr</a></li><li><a href="http://twitter.com/meyerweb/" rel="me">Twitter</a></li><li><a href="http://dopplr.com/traveller/meyerweb">Dopplr</a></li><li><a href="http://www.linkedin.com/in/meyerweb" rel="me">LinkedIn</a></li><li><a href="http://technorati.com/profile/emeyer" rel="me">Technorati</a></li></ul></div><div class="panel" id="pointers"><h4>Projects Elsewhere</h4><ul><li><a href="http://aneventapart.com/">An Event Apart</a></li><li><a href="http://complexspiral.com/">Complex Spiral Consulting</a></li><li><a href="http://www.webassist.com/go/css/emeyer/">CSS Sculptor</a></li><li><a href="http://css-discuss.org/">css-discuss</a></li><li><a href="http://microformats.org/">Microformats</a></li><li><a href="http://s5project.org/">S5</a></li></ul></div><div class="panel" id="tour"><ul><li><a href="http://fray.com/issue3/"><img src="http://fray.com/images/i3c.gif" alt="Fray Contributor (Issue 3: Sex &amp; Death)" /></a></li><!-- <li><a href="http://www.webassist.com/go/css/emeyer/"><img src="/pix/CS_ad_180x109.jpg" alt="CSS Sculptor for Dreamweaver" style="max-width: 100%;" /></a></li> --></ul></div><div class="panel">
<h4>Recently Tweeted</h4>
<p class="more"><a href="http://twitter.com/meyerweb">see more</a></p>
<p>About once a year, I de-clutter my office.  I usually do it in the spring because the snow shovel is no longer needed elsewhere. <small>&#8211;tweeted 7 hours, 16 minutes ago</small></p>
</div><div id="sideblog" class="panel">
<h4>Distractions</h4>
<p class="more">
<a href="/eric/thoughts/recent-links/">archive</a>
</p>
<ul>
<li><a href="http://8bitnyc.com/" title="March 17 | All of a sudden I want to establish a mission in Central Park and negotiate with the natives for gold and food.">8-Bit NYC</a></li>
<li><a href="http://www.youtube.com/watch?v=nFicqklGuB0&amp;feature=player_embedded" title="March 12 | Wry comment expressing my appreciation of the creative derivativeness of this video and its uncanny accuracy in mocking common tropes.">Academy Award Winning Movie Trailer</a></li>
<li><a href="http://www.youtube.com/watch?v=414TmP12WAU" title="March 9 | &#8220;Apple juice&#8230; for half price!&#8221;  More like twice PRICELESS.  (Note: If you&#8217;re at work, don your headphones.)">Happy in Paraguay</a> <small>[via <a href="http://unstoppablerobotninja.com/">Ethan</a>]</small></li>
<li><a href="http://www.youtube.com/watch?v=9V5ubAOeOBk&amp;feature=player_embedded" title="February 10 | This is approximately the best thing ever.">U900 -Walk Don&#8217;t Run (Isogabamaware)</a></li>
<li><a href="http://www.456bereastreet.com/archive/201002/sifr_default_css_hides_content_from_at_least_one_screen_reader/?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A 456bereastreet %28456 Berea Street%29" title="February 8 | -9999px comes through again, but I really wish we were beyond that kind of thing.">sIFR default CSS hides content from at least one screen reader</a></li>
<li><a href="http://www.macosxhints.com/article.php?story=20100117064356428" title="February 8 | Storing this for future use.">Take a picture with the iSight camera when a folder is opened</a></li>
<li><a href="http://mingle2.com/blog/view/web-developer-mind" title="February 4 | Mostly valid.  (SEE WHAT I DID THERE?)">The Mind of a Web Developer: An Illustrated Diagram</a></li>
<li><a href="http://www.theonion.com/content/news/science_channel_refuses_to_dumb" title="January 28 | &#8220;Punkin Chunkin, for Christ&#8217;s sake&#8230; What more do you people want?&#8221;">Science Channel Refuses To Dumb Down Science Any Further</a></li>
<li><a href="http://www.mailchimp.com/blog/project-omnivore-declassified/" title="January 27 | Sounds like quite a feat.  But I wonder how we&#8217;d feel if Microsoft or Google announced the same kind of thing on their e-mail services.">MailChimp&#8217;s Project Omnivore: Declassified</a></li>
<li><a href="http://www.politifact.com/truth-o-meter/statements/2010/jan/25/carolyn-maloney/congresswoman-says-democratic-presidents-create-mo/" title="January 26 | &#8220;Obviously, luck matters a lot, but when there is a consistent pattern over more than 60 years, it starts to look like more than just luck.&#8221;">Congresswoman says Democratic presidents create more private-sector jobs</a></li>
<li><a href="http://www.ted.com/talks/taylor_mali_what_teachers_make.html" title="January 25 | Truth.">Taylor Mali: What teachers make</a></li>
<li><a href="http://notebook.johnmartz.com/how-websites-work?c=1" title="January 22 | At last, the truth is out and I can stop pretending:  beatific monkeys are what makes it all go.">How websites work</a></li>
<li><a href="http://morsecode.scphillips.com/jtranslator.html" title="January 22 | &#8211; &#8230;. .. &#8230; / .. &#8230; / .- .&#8211; . &#8230; &#8212; &#8212; . / -. &#8212; / &#8230; . .-. .. &#8212; ..- &#8230; .-.. -.&#8211;">Morse Code Translator</a></li>
</ul>
</div>
<div class="panel" id="advisory">
<div class="guarded">
<a href="http://blogadvisorysystem.com/"><img src="/pix/bas/guarded.png" alt="Blog Advisory System Alert Level: Guarded"></a>
</div>
</div>

<div class="panel" id="excuse">
<h4>The <a href="/feeds/excuse/">excuse of the day</a> is</h4>
<p>failure of the AE-35 unit</p>
</div>

<div class="panel" id="extras">
<h4>Extras</h4>
<ul>
<li><a href="/feeds/">Feeds</a> &#8226;</li>
<li><a href="/eric/faq.html">FAQ</a> &#8226;</li>
<li><a href="/family.html">Family</a></li>
</ul>
</div>

</div>

<div id="navigate">
<h4>Navigation</h4>
<ul id="navlinks">
<li id="archLink"><a href="/eric/thoughts/">Archives</a></li>
<li id="cssLink"><a href="/eric/css/">CSS</a></li>
<li id="toolsLink"><a href="/eric/tools/">Toolbox</a></li>
<li id="writeLink"><a href="/eric/writing.html">Writing</a></li>
<li id="speakLink"><a href="/eric/talks/">Speaking</a></li>
<li id="otherLink"><a href="/other/">Leftovers</a></li>
<li id="aboutsite"><a href="/ui/about.html">About this site</a></li>
</ul>
</div>

<div id="footer">
<p class="sosumi">All contents of this site, unless otherwise noted, are &copy;1995-2008 <strong>Eric A. and Kathryn S. Meyer</strong>.  All Rights Reserved.</p>
<p>"<a href="/eric/thoughts/">Thoughts From Eric</a>" is powered by the &uuml;bercool <a href="http://wordpress.org/">WordPress</a></p>
</div>
</body>
</html>
