<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dev.enekoalonso.com &#187; jquery</title>
	<atom:link href="http://dev.enekoalonso.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.enekoalonso.com</link>
	<description>having fun with code</description>
	<lastBuildDate>Sat, 31 Jul 2010 05:51:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Creating HTML blocks with Mootools</title>
		<link>http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/</link>
		<comments>http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:50:16 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[implement]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=376</guid>
		<description><![CDATA[Sometimes we need to create HTML from JS. There are multiple ways to do this, from using innerHTML to creating element by element appending child to parents, setting attributes, etc. And there are as many different opinions about what is the right way to do it. Meanwhile, once thing I missed from jQuery, not available [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to create HTML from JS. There are multiple ways to do this, from using innerHTML to creating element by element appending child to parents, setting attributes, etc. And there are as many different opinions about what is the right way to do it.</p>
<p>Meanwhile, once thing I missed from jQuery, not available on Mootools, was the ability to do something like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> element = jQuery<span class="br0">&#40;</span><span class="st0">&#39;&lt;div id=&quot;foo&quot;&gt;bar&lt;/div&gt;&#39;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>In Mootools, the equivalent should be:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> element = <span class="kw2">new</span> Element<span class="br0">&#40;</span><span class="st0">&#39;&lt;div id=&quot;foo&quot;&gt;bar&lt;/div&gt;&#39;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>But that does not work.</p>
<p>On the Mootools mailing list, someone was asking about how to implement this and we just found a different approach, which instead of using the Element class, extends the String class like this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">String.<span class="me1">implement</span><span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;toElement: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">new</span> Element<span class="br0">&#40;</span><span class="st0">&#39;div&#39;</span>, <span class="br0">&#123;</span>html:<span class="kw1">this</span><span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">getFirst</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now you can do this:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> element &nbsp;= <span class="st0">&#39;&lt;div id=&quot;foo&quot;&gt;bar&lt;/div&gt;&#39;</span>.<span class="me1">toElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>To try it, paste this snippet on Firebug&#8217;s console:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">String.<span class="me1">implement</span><span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;toElement: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">new</span> Element<span class="br0">&#40;</span><span class="st0">&#39;div&#39;</span>, <span class="br0">&#123;</span>html:<span class="kw1">this</span><span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">getFirst</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&#39;&lt;div id=&quot;foo&quot;&gt;bar&lt;/div&gt;&#39;</span>.<span class="me1">toElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">&#39;id&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Nice, uh? Of course, the HTML can be as complex as you need and where do you get the content of the string is up to you :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>March 24, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/24/mooml-1-1-no-more-eval-say-hi-to-mooml-template-and-mooml-templates/" title="Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates">Mooml 1.1 &#8211; No more eval(), say Hi to Mooml.Template and Mooml.Templates</a> (0)</li><li>July 20, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/" title="Little tricks: string padding in Javascript">Little tricks: string padding in Javascript</a> (2)</li><li>July 1, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/" title="Little tricks: editing strings by index in Javascript and Python">Little tricks: editing strings by index in Javascript and Python</a> (1)</li><li>April 28, 2010 -- <a href="http://dev.enekoalonso.com/2010/04/28/mooml-1-2-3-bye-bye-with/" title="Mooml 1.2.3 &#8211; Bye, bye, with()">Mooml 1.2.3 &#8211; Bye, bye, with()</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Keyboard shortcuts in Drupal: Navigating through comments</title>
		<link>http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/</link>
		<comments>http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 17:44:19 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[keydown]]></category>
		<category><![CDATA[keyup]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=148</guid>
		<description><![CDATA[It was one of the best features of Google Reader and Google GMail: the keyboard shortcuts, specially the ones to navigate through messages or posts (n = next, p = previous). I decided it was time to implement it on spaniards.es, so here is the code I used. So far it only navigates through comments [...]]]></description>
			<content:encoded><![CDATA[<p>It was one of the best features of Google Reader and Google GMail: the keyboard shortcuts, specially the ones to navigate through messages or posts (n = next, p = previous).</p>
<p>I decided it was time to implement it on <a href="http://www.spaniards.es">spaniards.es</a>, so here is the code I used. So far it only navigates through comments back and forth on the same page (does not go to next page or so). I am planning to add other shortcuts soon.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">keyup</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>event.<span class="me1">target</span>.<span class="me1">tagName</span> == <span class="st0">&quot;INPUT&quot;</span> <span class="sy0">||</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; event.<span class="me1">target</span>.<span class="me1">tagName</span> == <span class="st0">&quot;TEXTAREA&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> selected = $<span class="br0">&#40;</span><span class="st0">&#39;.comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Next comment</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>event.<span class="me1">keyCode</span> == <span class="nu0">78</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selected.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected.<span class="me1">removeClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected = selected.<span class="me1">nextAll</span><span class="br0">&#40;</span><span class="st0">&#39;.comment:first&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected = $<span class="br0">&#40;</span><span class="st0">&#39;.comment:first&#39;</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selected.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; window.<span class="me1">scrollTo</span><span class="br0">&#40;</span><span class="nu0">0</span>, selected.<span class="me1">offset</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">top</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Previous comment</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>event.<span class="me1">keyCode</span> == <span class="nu0">80</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selected.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected.<span class="me1">removeClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected = selected.<span class="me1">prevAll</span><span class="br0">&#40;</span><span class="st0">&#39;.comment:first&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; selected = $<span class="br0">&#40;</span><span class="st0">&#39;.comment:last&#39;</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;comment-selected&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selected.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; window.<span class="me1">scrollTo</span><span class="br0">&#40;</span><span class="nu0">0</span>, selected.<span class="me1">offset</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">top</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>As you can see, it is important to leave the input and textarea fields alone, so the keyboard shortcuts don&#8217;t interfere with the user typing some text. With a little bit of CSS the selected comment can be displayed with a different border color or background. And that&#8217;s it!</p>
<p>Check an example topic at <a href="http://www.spaniards.es/foros/2008/12/12/empresas-alemanas-que-trabajen-ingles">www.spaniards.es</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 13, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part III">Ajax comments in Drupal 5: How I made it &#8211; Part III</a> (10)</li><li>December 11, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part II">Ajax comments in Drupal 5: How I made it &#8211; Part II</a> (0)</li><li>December 10, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/" title="Ajax comments in Drupal 5: How I made it &#8211; Part I">Ajax comments in Drupal 5: How I made it &#8211; Part I</a> (7)</li><li>February 24, 2009 -- <a href="http://dev.enekoalonso.com/2009/02/24/vancode2int/" title="vancode2int">vancode2int</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ajax comments in Drupal 5: How I made it &#8211; Part III</title>
		<link>http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/</link>
		<comments>http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 20:42:11 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=128</guid>
		<description><![CDATA[In Ajax comments in Drupal 5: How I made it parts I and II we saw the code needed on the back-end. Now, let&#8217;s see what goes on the client side: the Javascript. Submitting the comment with an ajax request Basically, to add a comment we need the node id (nid) and the comment text. [...]]]></description>
			<content:encoded><![CDATA[<p>In Ajax comments in Drupal 5: How I made it <a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-i">parts I</a> and <a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii">II</a> we saw the code needed on the back-end. Now, let&#8217;s see what goes on the client side: the Javascript.</p>
<h3>Submitting the comment with an ajax request</h3>
<p>Basically, to add a comment we need the node id (nid) and the comment text. The code in the back-end will handle the user info and permissions. The first thing we do is find the comment form and the submit button. Then we hook the click event on the submit button.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> commentForm = jQuery<span class="br0">&#40;</span><span class="st0">&#39;form#comment-form&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>commentForm.<span class="me1">length</span><span class="br0">&#41;</span> <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> submitBtn = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-submit&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>submitBtn.<span class="me1">length</span><span class="br0">&#41;</span> <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; submitBtn.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>ev<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// &#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>When the user clicks the submit button, we get the node id (we could have done on domready) and the comment text. If the comment is empty, we don&#8217;t do anything, so the comment will be submitted the standard way and Drupal will fail the comment form validation.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get Node Id</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> nid = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#node-nid&#39;</span><span class="br0">&#41;</span>.<span class="me1">text</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>nid<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>; <span class="co1">// Do nothing&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span>nid<span class="br0">&#41;</span> <span class="sy0">&lt;</span>= <span class="nu0">0</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get Comment Text</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> text = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-comment&#39;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>text<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>; <span class="co1">// Let Drupal validate the form</span></div>
</li>
</ol>
</div>
<p>Since we don&#8217;t want to get back just the comment added but all the new comments added by any user on this node, we want to pass the last comment id on screen.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get last comment Id</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> lastComment = jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.comment:last&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> lastCommentId = <span class="br0">&#40;</span>lastComment.<span class="me1">length</span><span class="br0">&#41;</span>? lastComment.<span class="me1">id</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&#39;-&#39;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> : <span class="nu0">0</span>;</div>
</li>
</ol>
</div>
<p>Due to my limited knowledge of jQuery at the time, I used the load function for the ajax request. The problem with this function is that, from what I know, it only works on elements already included in the DOM. So before the request, I had to create a container for the new comments. The point of insertion will differ if the page already has comments or not:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We got both nid and text. Let&#39;s create the comment holder</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> comment = jQuery<span class="br0">&#40;</span>document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;div&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; comment.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>lastComment.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; lastComment.<span class="me1">after</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; comment.<span class="me1">insertBefore</span><span class="br0">&#40;</span>jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.box&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Finally, the load request:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Ajax submit/load</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; comment.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">&#39;/add/comment&#39;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> &nbsp;<span class="st0">&#39;nid&#39;</span>: nid, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;text&#39;</span>: text,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;lastcid&#39;</span>: lastCommentId </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>responseText, textStatus, XMLHttpRequest<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>textStatus == <span class="st0">&#39;success&#39;</span> <span class="sy0">&amp;&amp;</span> responseText<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ajax_insert_comment<span class="br0">&#40;</span>comment<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comment.<span class="me1">remove</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Error: No se pudo enviar el comentario&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>If the request failed, the container was removed. If the request succeeded, the new comments were added to the page:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">ajax_insert_comment = <span class="kw2">function</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Insert new comments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; comment.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>And that was it! Ajax comments in Drupal working like a charm.</p>
<h3>Adding/removing the &#8220;new&#8221; indicator</h3>
<p>Now that I had my Ajax comments working I wanted them to look better. For example, I wanted the new added comments to have the &#8220;new&#8221; indicator, while at the same time I wanted to remove the indicator from the comments already on the screen. On ajax_insert_comment:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Clear &#39;new&#39; from previous comments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.comment&#39;</span><span class="br0">&#41;</span>.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#39;span.new&#39;</span><span class="br0">&#41;</span>.<span class="me1">remove</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add &#39;new&#39; indicator</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> newcomment = jQuery<span class="br0">&#40;</span>document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;span&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; newcomment.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;new&#39;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span><span class="st0">&#39;new&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; newcomment.<span class="me1">insertBefore</span><span class="br0">&#40;</span>comment.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#39;div.comment-author&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And many other tweaks like that.</p>
<h3>The code</h3>
<p>Here is the whole code for ajax comment submission I currently use:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">ajax_comments = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> commentForm = jQuery<span class="br0">&#40;</span><span class="st0">&#39;form#comment-form&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>commentForm.<span class="me1">length</span><span class="br0">&#41;</span> <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> submitBtn = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-submit&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>submitBtn.<span class="me1">length</span><span class="br0">&#41;</span> <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; submitBtn.<span class="me1">val</span><span class="br0">&#40;</span><span class="st0">&#39;Enviar comentario&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; submitBtn.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>ev<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Disable submit button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; submitBtn.<span class="me1">val</span><span class="br0">&#40;</span><span class="st0">&#39;Enviando&#8230;&#39;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;disabled&quot;</span>,<span class="st0">&quot;disabled&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Clear the &#39;edit&#39; link from previous comments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#39;li.edit-comment-link&#39;</span><span class="br0">&#41;</span>.<span class="me1">remove</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get Node Id</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> nid = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#node-nid&#39;</span><span class="br0">&#41;</span>.<span class="me1">text</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>nid<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>; <span class="co1">// Do nothing&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span>nid<span class="br0">&#41;</span> <span class="sy0">&lt;</span>= <span class="nu0">0</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get Comment Text</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> text = jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-comment&#39;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>text<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">true</span>; <span class="co1">// Let Drupal validate the form</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Get last comment Id</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> lastComment = jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.comment:last&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> lastCommentId = <span class="br0">&#40;</span>lastComment.<span class="me1">length</span><span class="br0">&#41;</span>? lastComment.<span class="me1">id</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&#39;-&#39;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> : <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// // We got both nid and text. Let&#39;s create the comment holder</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> comment = jQuery<span class="br0">&#40;</span>document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;div&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; comment.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>lastComment.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; lastComment.<span class="me1">after</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; comment.<span class="me1">insertBefore</span><span class="br0">&#40;</span>jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.box&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Ajax submit/load</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; comment.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">&#39;/add/comment&#39;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> &nbsp;<span class="st0">&#39;nid&#39;</span>: nid, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;text&#39;</span>: text,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;lastcid&#39;</span>: lastCommentId </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>responseText, textStatus, XMLHttpRequest<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>textStatus == <span class="st0">&#39;success&#39;</span> <span class="sy0">&amp;&amp;</span> responseText<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ajax_insert_comment<span class="br0">&#40;</span>comment<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-comment&#39;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="st0">&#39;&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comment.<span class="me1">remove</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Error: No se pudo enviar el comentario&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Reenable submit button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; submitBtn.<span class="me1">val</span><span class="br0">&#40;</span><span class="st0">&#39;Enviar comentario&#39;</span><span class="br0">&#41;</span>.<span class="me1">removeAttr</span><span class="br0">&#40;</span><span class="st0">&quot;disabled&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">ajax_insert_comment = <span class="kw2">function</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Clear &#39;new&#39; from previous comments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#39;div.comment&#39;</span><span class="br0">&#41;</span>.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#39;span.new&#39;</span><span class="br0">&#41;</span>.<span class="me1">remove</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add &#39;new&#39; indicator</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> newcomment = jQuery<span class="br0">&#40;</span>document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;span&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; newcomment.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&#39;new&#39;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span><span class="st0">&#39;new&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; newcomment.<span class="me1">insertBefore</span><span class="br0">&#40;</span>comment.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#39;div.comment-author&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Remove comment count</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; comment.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">&#39;span.commentcount&#39;</span><span class="br0">&#41;</span>.<span class="me1">empty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Insert new comments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; comment.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#39;#edit-comment&#39;</span><span class="br0">&#41;</span>.<span class="kw3">focus</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">jQuery<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; ajax_comments<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>As you can see, when I wrote this code back in July, I have just started using jQuery and I didn&#8217;t know much about event handling. So I used the old return true/false to stop the submit event if needed. Also, now that I see the code again I can think on better ways to do parts of it. Well, that&#8217;s a good sign: it means I&#8217;m getting better :)</p>
<p>Series:<br />
<a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/">Ajax comments in Drupal 5: How I made it &#8211; Part I</a><br />
<a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/">Ajax comments in Drupal 5: How I made it &#8211; Part II</a><br />
<a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/">Ajax comments in Drupal 5: How I made it &#8211; Part III</a></p>
<h3>See the ajax comments in action</h3>
<p>Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is <a href="http://www.spaniards.es">www.spaniards.es</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 11, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part II">Ajax comments in Drupal 5: How I made it &#8211; Part II</a> (0)</li><li>December 10, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/" title="Ajax comments in Drupal 5: How I made it &#8211; Part I">Ajax comments in Drupal 5: How I made it &#8211; Part I</a> (7)</li><li>December 14, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/" title="Keyboard shortcuts in Drupal: Navigating through comments">Keyboard shortcuts in Drupal: Navigating through comments</a> (2)</li><li>February 24, 2009 -- <a href="http://dev.enekoalonso.com/2009/02/24/vancode2int/" title="vancode2int">vancode2int</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Ajax comments in Drupal 5: How I made it &#8211; Part II</title>
		<link>http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/</link>
		<comments>http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 04:48:54 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=104</guid>
		<description><![CDATA[In Axaj comments in Drupal 5: How I made it &#8211; Part I I covered the most important part of the module code: saving comments to the database. Now, let&#8217;s see how to display them properly. Rendering the new comment Comments usually have more information than the user name and the comment text. In my [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i">Axaj comments in Drupal 5: How I made it &#8211; Part I</a> I covered the most important part of the module code: saving comments to the database. Now, let&#8217;s see how to display them properly.</p>
<h3>Rendering the new comment</h3>
<p>Comments usually have more information than the user name and the comment text. In my website, for instance, comments have information like the user registered date, where he/she lives, the country flag, the comment number on the thread, etc. All this information could not be made up at the client side; the server had to send it back to the client once the comment was saved.</p>
<p>Also, we want to take full advantage of Drupal: we want the corresponding filters applied to the comment text to remove malicious HTML tags, convert plain URLs into HTML links, etc.   </p>
<p>The best way to do this is to use the function theme with the parameter &#8216;comment_view&#8217;. The difference between theme(&#8216;comment&#8217;,&#8230;) and theme(&#8216;comment_view&#8217;,&#8230;) is that filters are applied only in the second case, if I am not mistaken. </p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> _spaniards_get_latest_comments<span class="br0">&#40;</span><span class="re1">$nid</span><span class="sy0">,</span> <span class="re1">$fromcid</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$result</span> <span class="sy0">=</span> db_query<span class="br0">&#40;</span><span class="st0">&#39;select * from comments where nid = %d and cid &gt; %d&#39;</span><span class="sy0">,</span> <span class="re1">$nid</span><span class="sy0">,</span> <span class="re1">$fromcid</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$content</span> <span class="sy0">=</span> <span class="st0">&#39;&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="re1">$comment</span> <span class="sy0">=</span> db_fetch_object<span class="br0">&#40;</span><span class="re1">$result</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$comment</span><span class="sy0">-&gt;</span><span class="me1">status</span> <span class="sy0">==</span> COMMENT_NOT_PUBLISHED<span class="br0">&#41;</span> <span class="kw1">continue</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">$links</span> <span class="sy0">=</span> module_invoke_all<span class="br0">&#40;</span><span class="st0">&#39;link&#39;</span><span class="sy0">,</span> <span class="st0">&#39;comment&#39;</span><span class="sy0">,</span> <span class="re1">$comment</span><span class="sy0">,</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">$content</span> <span class="sy0">.=</span> theme<span class="br0">&#40;</span><span class="st0">&#39;comment_view&#39;</span><span class="sy0">,</span> <span class="re1">$comment</span><span class="sy0">,</span> <span class="re1">$links</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re1">$content</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>As I mentioned before, we don&#8217;t want to return just the comment added by the user but any comment added by any other user since the user loaded the page. This function will return any comment on the thread with comment Id greater than the parameter $fromcid.</p>
<p>Besides using theme(&#8216;comment_view&#8217;,&#8230;) it is a good idea also to invoke the link hook. This will let other modules like <a href="http://drupal.org/project/fasttoggle">fasttoggle</a> inject their very useful links in out comment.</p>
<p>And that is it for now. This code, together with the code on Part I will be the only PHP needed. We will see the client side Javascript code on Part III.</p>
<p>Series:<br />
<a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/">Ajax comments in Drupal 5: How I made it &#8211; Part I</a><br />
<a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/">Ajax comments in Drupal 5: How I made it &#8211; Part II</a><br />
<a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/">Ajax comments in Drupal 5: How I made it &#8211; Part III</a></p>
<h3>See the ajax comments in action</h3>
<p>Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is <a href="http://www.spaniards.es">www.spaniards.es</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 13, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part III">Ajax comments in Drupal 5: How I made it &#8211; Part III</a> (10)</li><li>December 10, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/" title="Ajax comments in Drupal 5: How I made it &#8211; Part I">Ajax comments in Drupal 5: How I made it &#8211; Part I</a> (7)</li><li>December 14, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/" title="Keyboard shortcuts in Drupal: Navigating through comments">Keyboard shortcuts in Drupal: Navigating through comments</a> (2)</li><li>February 24, 2009 -- <a href="http://dev.enekoalonso.com/2009/02/24/vancode2int/" title="vancode2int">vancode2int</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax comments in Drupal 5: How I made it &#8211; Part I</title>
		<link>http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/</link>
		<comments>http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 04:29:43 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=95</guid>
		<description><![CDATA[First of all, I would like to apologize for not publishing this the right way. I haven&#8217;t had time to put it all on a standalone module, which will make it easier to share the code at www.drupal.org. Feel free to grab the code and put it on a module if you wish (and let [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, I would like to apologize for not publishing this the right way. I haven&#8217;t had time to put it all on a standalone module, which will make it easier to share the code at <a href="http://www.drupal.org">www.drupal.org</a>. Feel free to grab the code and put it on a module if you wish (and let me know if you do so :)</p>
<h3>Why Ajax comments?</h3>
<p>Ajax comments let users to submit comments without having to reload the whole page. Reloading a page is not only slow, but its an overload for the server which has to process and send the whole page again just to add a simple comment to the DOM. So yes, Ajax comments are a big benefit, both for the user, which has to wait less, and for the server, which has to process and send less information.</p>
<h3>Available modules</h3>
<p>Point explained, lets see how it went. In the past I tried the <a href="http://drupal.org/project/jstools">Javascript Tools</a> set of modules, which had other dependencies if I am not mistaken. And I don&#8217;t know why but it didn&#8217;t work at all for me. My comments kept reloading the whole page while sent. A couple of months ago I decided it was time to do it myself.</p>
<h3>The plan</h3>
<p>My original plan was simple: when the user hits the submit button, send the comment to the server, save it and add it to the DOM at the end of the comment list. This looked simple, but it had a few thing to deal with. I didn&#8217;t want to mess with Drupal&#8217;s comment module neither modify any line of Drupal&#8217;s core. So I decided to take an alternative path.</p>
<h3>Saving a comment</h3>
<p>Saving a comment doesn&#8217;t require much information. Since the user has to be logged in, the only information needed is basically the node Id and the comment itself. Drupal has a very nice hook_menu that allows you to create pages with a specific URL. These pages can process data, serve data or both. The only drawback, if so, was that hooks only works on modules, so I had to create my own. I decided to add it to my custom site module.</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> spaniards_menu<span class="br0">&#40;</span><span class="re1">$may_cache</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$items</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="re1">$may_cache</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Add comment</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">$items</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;path&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;add/comment&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;title&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;Add Comment&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;callback&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;spaniards_add_comment&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;callback arguments&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;access&#39;</span> <span class="sy0">=&gt;</span> user_access<span class="br0">&#40;</span><span class="st0">&quot;post comments&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&#39;type&#39;</span> <span class="sy0">=&gt;</span> MENU_CALLBACK<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> spaniards_add_comment<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">global</span> <span class="re1">$user</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$text</span> <span class="sy0">=</span> <span class="re1">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#39;text&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;uid&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re1">$user</span><span class="sy0">-&gt;</span><span class="me1">uid</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;nid&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re1">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#39;nid&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;pid&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;subject&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">substr</span><span class="br0">&#40;</span><span class="re1">$text</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">20</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;comment&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re1">$text</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Add comment to DB</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$cid</span> <span class="sy0">=</span> comment_save<span class="br0">&#40;</span><span class="re1">$comment</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Mark the node as viewed by the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;node_tag_new<span class="br0">&#40;</span><span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;nid&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Get all comments since the last one</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$fromcid</span> <span class="sy0">=</span> <span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#39;lastcid&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>? <span class="re1">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#39;lastcid&#39;</span><span class="br0">&#93;</span> <span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">print</span> _spaniards_get_latest_comments<span class="br0">&#40;</span><span class="re1">$comment</span><span class="br0">&#91;</span><span class="st0">&#39;nid&#39;</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re1">$fromcid</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">exit</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Just with those two functions we can bypass Drupal&#8217;s comment submission system and add new comments to the database. As long as we use the comment_save function, the actual process involved in saving a comment is safe (hooks get called, watchdog logging works, etc). Also, hook_menu guarantees only requests with the right access will be able to add a comment (in this case the user_access function verifies the user has &#8220;post comments&#8221; permissions granted).</p>
<p>Once the comment is saved, the node is marked as viewed by the user, to remove all &#8220;new&#8221; labels from the new comments added.</p>
<p>Finally the comment is rendered and returned to the user. But wait! What if another user has posted a comment in between? During a conversation between multiple users this will happen very often. The solution: return all comments added since the last comment visible when the user visited the thread.</p>
<p>We will see how to do that next.</p>
<p>Series:<br />
<a href="http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/">Ajax comments in Drupal 5: How I made it &#8211; Part I</a><br />
<a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/">Ajax comments in Drupal 5: How I made it &#8211; Part II</a><br />
<a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/">Ajax comments in Drupal 5: How I made it &#8211; Part III</a></p>
<h3>See the ajax comments in action</h3>
<p>Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is <a href="http://www.spaniards.es">www.spaniards.es</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 13, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/13/ajax-comments-in-drupal-5-how-i-made-it-part-iii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part III">Ajax comments in Drupal 5: How I made it &#8211; Part III</a> (10)</li><li>December 11, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/11/ajax-comments-in-drupal-5-how-i-made-it-part-ii/" title="Ajax comments in Drupal 5: How I made it &#8211; Part II">Ajax comments in Drupal 5: How I made it &#8211; Part II</a> (0)</li><li>December 14, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/14/keyboard-shortcuts-in-drupal-navigating-through-comments/" title="Keyboard shortcuts in Drupal: Navigating through comments">Keyboard shortcuts in Drupal: Navigating through comments</a> (2)</li><li>February 24, 2009 -- <a href="http://dev.enekoalonso.com/2009/02/24/vancode2int/" title="vancode2int">vancode2int</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/10/ajax-comments-in-drupal-5-how-i-made-it-part-i/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Highlighting elements with Firebug console</title>
		<link>http://dev.enekoalonso.com/2008/12/02/highlighting-elements-with-firebug-console/</link>
		<comments>http://dev.enekoalonso.com/2008/12/02/highlighting-elements-with-firebug-console/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 19:24:27 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[prototypejs]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=74</guid>
		<description><![CDATA[Today I had an issue with an element was breaking a page layout, making the page width wider than 960px. I tried to find it with Firebug, looking at the layout properties of some divs and elements, but I couldn&#8217;t find it. I knew it had to be there, so I decided to highlight all [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had an issue with an element was breaking a page layout, making the page width wider than 960px. I tried to find it with Firebug, looking at the layout properties of some divs and elements, but I couldn&#8217;t find it. I knew it had to be there, so I decided to highlight all div elements in the page with a red border. And it worked! I found it right away.</p>
<p>Here is the code I used to highlight the fields (I had to do it in prototype only):</p>
<p>Prototype JS:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>img src=<span class="st0">&quot;http://l.wordpress.com/latex.php?latex=%28%27div%27%29.each%28function%28item%29%7B%20%20%24%28item%29.setStyle%28%7B%20border%3A%20%27solid%201px%20red%27%20%7D%29%3B%7D%29%3B%3C%2Fpre%3EjQuery%3A%3Cpre%20lang%3D%22javascript%22%3E%24%28%27div%27%29.css%28%27border%27%2C%20%27solid%201px%20red%27%29%3B%3C%2Fpre%3EMootools%3A%3Cpre%20lang%3D%22javascript%22%3E&amp;bg=0D324F&amp;fg=FFFFFF&amp;s=1&quot;</span> title=<span class="st0">&quot;(&#39;div&#39;).each(function(item){ &nbsp;$(item).setStyle({ border: &#39;solid 1px red&#39; });});</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"</span></div>
</li>
</ol>
</div>
<p>jQuery:
<pre lang="javascript">$('div').css('border', 'solid 1px red');</pre>
<p>Mootools:
<pre lang="javascript">" style="vertical-align:-20%;" class="tex" alt="('div').each(function(item){  $(item).setStyle({ border: 'solid 1px red' });});</pre>
<p>jQuery:
<pre lang="javascript">$('div').css('border', 'solid 1px red');</pre>
<p>Mootools:
<pre lang="javascript">" />('div').setStyle('border', 'solid 1px red');
</pre>
<p>PS: Note that by adding a 1px border to all divs in the page, the page layout will be affected and some items may display improperly. Of course, you can use other colors than red :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>November 30, 2008 -- <a href="http://dev.enekoalonso.com/2008/11/30/injecting-javascript-with-firebug/" title="Injecting javascript with Firebug">Injecting javascript with Firebug</a> (0)</li><li>September 2, 2009 -- <a href="http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/" title="Creating HTML blocks with Mootools">Creating HTML blocks with Mootools</a> (3)</li><li>November 29, 2008 -- <a href="http://dev.enekoalonso.com/2008/11/29/jsclass-a-very-nice-object-oriented-approach/" title="JS.Class: a very nice object oriented approach">JS.Class: a very nice object oriented approach</a> (0)</li><li>May 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/" title="Interesting JSON vulnerability (old stuff)">Interesting JSON vulnerability (old stuff)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/02/highlighting-elements-with-firebug-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting javascript with Firebug</title>
		<link>http://dev.enekoalonso.com/2008/11/30/injecting-javascript-with-firebug/</link>
		<comments>http://dev.enekoalonso.com/2008/11/30/injecting-javascript-with-firebug/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 05:29:22 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=64</guid>
		<description><![CDATA[Sometimes you may want to include a Javascript file on a live web page to see how things will work or to try new things. Usually you would do this by editing the source code including the new file but there is a fastest way if you have Firefox with Firebug. Just run the following [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you may want to include a Javascript file on a live web page to see how things will work or to try new things. Usually you would do this by editing the source code including the new file but there is a fastest way if you have Firefox with Firebug. Just run the following code in the Javascript console:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> headID = document.<span class="me1">getElementsByTagName</span><span class="br0">&#40;</span><span class="st0">&quot;head&quot;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> newScript = document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#39;script&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">newScript.<span class="me1">type</span> = <span class="st0">&#39;text/javascript&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">newScript.<span class="me1">src</span> = <span class="st0">&#39;http://enekoalonso.com/lib/jquery-1.2.6.min.js&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">headID.<span class="me1">appendChild</span><span class="br0">&#40;</span>newScript<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Here I am loading the jQuery library from my own server, but I could load any Javascript from any server on the net. The best is this will work on any website, whenever you have access to the server or not. Once you have the Javascript loaded you can use it right away. For example, here on this blog I could use jQuery to check how the header will look if its height was only 100px by running the next command on the console:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">jQuery<span class="br0">&#40;</span><span class="st0">&#39;#header&#39;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">&#39;height&#39;</span>,<span class="nu0">100</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Yep, it won&#8217;t look very good, right? Try it! You have to see it by yourself. Firefox is great. Firebug is awesome. And Firebug&#8217;s Javascript console is the best thing ever!<br />
PS: In case you are wondering, the Javascript file injected will exist only on the current browser session.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 2, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/02/highlighting-elements-with-firebug-console/" title="Highlighting elements with Firebug console">Highlighting elements with Firebug console</a> (0)</li><li>January 20, 2010 -- <a href="http://dev.enekoalonso.com/2010/01/20/debugging-cookies-with-firebug-1-51-6/" title="Debugging cookies with Firebug 1.5/1.6">Debugging cookies with Firebug 1.5/1.6</a> (0)</li><li>January 19, 2010 -- <a href="http://dev.enekoalonso.com/2010/01/19/firefox-3-6rc2-firebug-1-6a/" title="Firefox 3.6RC2 &#038; Firebug 1.6a">Firefox 3.6RC2 &#038; Firebug 1.6a</a> (0)</li><li>May 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/05/14/interesting-json-vulnerability-old-stuff/" title="Interesting JSON vulnerability (old stuff)">Interesting JSON vulnerability (old stuff)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/11/30/injecting-javascript-with-firebug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JS.Class: a very nice object oriented approach</title>
		<link>http://dev.enekoalonso.com/2008/11/29/jsclass-a-very-nice-object-oriented-approach/</link>
		<comments>http://dev.enekoalonso.com/2008/11/29/jsclass-a-very-nice-object-oriented-approach/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 18:32:37 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[js.class]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=57</guid>
		<description><![CDATA[I have been using jQuery and Mootools in the last few months at work, among other libraries. So far, jQuery was my favorite because it is very fast and encapsulated (it doesn&#8217;t interfere with other librearies, etc). But I love Mootools too because of its object oriented approach and beautiful things like the Class class [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using <a href="http://jquery.com/">jQuery</a> and <a href="http://mootools.net/">Mootools</a> in the last few months at work, among other libraries. So far, jQuery was my favorite because it is very fast and encapsulated (it doesn&#8217;t interfere with other librearies, etc). But I love Mootools too because of its object oriented approach and beautiful things like the Class class which lets you create JS classes very easily. Every time I use jQuery I miss that from Mootools.</p>
<p>Well, a copuple of days ago I have discovered <a href="http://jsclass.jcoglan.com/">JS.Class</a> which I think is the best thing I have seen so far in object oriented Javascript. It is awesome! Very well designed and documented and very well encapsulated.</p>
<p>JS.Class is not a library for DOM manipulation but just a framework to build object oriented code. And I love that! I think it is way better to focus on a single task, instead of trying to cover it all. This makes JS.Class a very lightweight library too :)</p>
<p><a href="http://dev.enekoalonso.com/2008/11/27/copy-constructor-test-in-javascript/">Copy-constructor test</a> class in Mootools:</p>
<div class="geshi no javascript">
<div class="head">var myClass = new Class({</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; text: <span class="st0">&#39;&#39;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; say: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">text</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p><a href="http://dev.enekoalonso.com/2008/11/27/copy-constructor-test-in-javascript/">Copy-constructor test</a> class in JS.Class:</p>
<div class="geshi no javascript">
<div class="head">var myClass = new JS.Class({</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; text: <span class="st0">&#39;&#39;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; say: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console.<span class="me1">log</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">text</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>As you can see, the way of creating classes is almost the same. So what makes JS.Class better than Mootools? Where, both allow inheritance and extending classes with other class-helpers, but JS.Class does this better. I&#8217;ll work on some example code to prove this :)</p>
<p>PS: Don&#8217;t take me wrong, I still love Mootools and I&#8217;ll still use it on web development. But I try to use JS.Class as much as possible.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>September 2, 2009 -- <a href="http://dev.enekoalonso.com/2009/09/02/creating-html-blocks-with-mootools/" title="Creating HTML blocks with Mootools">Creating HTML blocks with Mootools</a> (3)</li><li>December 2, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/02/highlighting-elements-with-firebug-console/" title="Highlighting elements with Firebug console">Highlighting elements with Firebug console</a> (0)</li><li>April 28, 2010 -- <a href="http://dev.enekoalonso.com/2010/04/28/mooml-1-2-3-bye-bye-with/" title="Mooml 1.2.3 &#8211; Bye, bye, with()">Mooml 1.2.3 &#8211; Bye, bye, with()</a> (0)</li><li>April 14, 2010 -- <a href="http://dev.enekoalonso.com/2010/04/14/getting-unique-timestamps-in-javascript/" title="Getting unique timestamps in Javascript">Getting unique timestamps in Javascript</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/11/29/jsclass-a-very-nice-object-oriented-approach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
