<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Enrique Delgado Monroy: Tag html</title>
    <link>http://www.enriquedelgado.com/articles/tag/html</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Work, Life, Stuff</description>
    <item>
      <title>Attaching functions to events in JavaScript: The unobstrusive way! - Part 2</title>
      <description>&lt;p&gt;&lt;a href="http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way"&gt;Previously&lt;/a&gt;, I talked about a good way to attach event listeners to &lt;span class="caps"&gt;DOM&lt;/span&gt; elements. I like the approach described in that post because is pure JavaScript and not dependent on a framework, but if you happen to be using a framework already, or are thinking on using some, here are two methods of achieving this task:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://prototypejs.org/api/event"&gt;Prototype.js&lt;/a&gt;: &lt;tt&gt;observe&lt;/tt&gt; method.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;: &lt;tt&gt;bind&lt;/tt&gt; method and friends.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;It is important to remember that, your code should wait for the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree to completely load before attempting to attach events to elements. The typical scenario is that some JavaScript code is attempting to attach an event to an element that does not exist yet, because the document is still loading.&lt;/p&gt;


	&lt;p&gt;Each framework has ways of wrapping your code, so that it runs only when the &lt;span class="caps"&gt;DOM&lt;/span&gt; is ready:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Prototype&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
document.observe("dom:loaded", function() {
  // Your code here
});
&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;jQuery&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
$(document).ready(function(){
   // Your code here
 });

&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using Prototype&lt;/h3&gt;


	&lt;p&gt;Prototype offers several event handling methods, but the most common is &lt;code&gt;observe&lt;/code&gt; and &lt;code&gt;stopObserving&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$('foo').observe('click', respondToClick);

function respondToClick(event) {
  // This is how you access the element that triggered the event:
  var element = event.element();
  // Your code here, e.g.:
  alert('Hello');
}
&lt;/pre&gt;

	&lt;p&gt;Notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. The complete list of events are defined in the &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings"&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; recommendations&lt;/a&gt;, but here is a summary of the most common event names organized by type:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Mouse Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;click&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is clicked over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousedown&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is pressed over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseup&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is released over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseover&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved onto an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousemove&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved while it is over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseout&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved away from an element.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;Key Events
	&lt;ul&gt;
	&lt;li&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; does not have them, but the following are supplied by Prototype:&lt;br /&gt;&lt;tt&gt;&lt;span class="caps"&gt;KEY&lt;/span&gt;_BACKSPACE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_TAB, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RETURN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_ESC, &lt;span class="caps"&gt;KEY&lt;/span&gt;_LEFT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_UP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RIGHT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DOWN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DELETE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_HOME, &lt;span class="caps"&gt;KEY&lt;/span&gt;_END, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEUP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEDOWN&lt;/tt&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt; Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;load&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation finishes loading.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;unload&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation removes a document from a window or frame.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;select&lt;/tt&gt; &amp;#8211; Occurs when a user selects some text in a text field.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;change&lt;/tt&gt; &amp;#8211; Occurs when a control loses the input focus and its value has been modified since gaining focus.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;submit&lt;/tt&gt; &amp;#8211; Occurs when a form is submitted.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;focus&lt;/tt&gt; &amp;#8211; Occurs when an element receives focus either via a pointing device or by tabbing navigation.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;blur&lt;/tt&gt; &amp;#8211; Occurs when an element loses focus either via the pointing device or by tabbing navigation.&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using jQuery&lt;/h3&gt;


	&lt;p&gt;jQuery mostly has &amp;#8220;helper methods&amp;#8221; to set event listeners that are named after the event name, but the two most basic methods are &lt;tt&gt;bind&lt;/tt&gt; and &lt;tt&gt;unbind&lt;/tt&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").bind("click", function(e){
  // This is how you access the element that triggered the event:
  var element = event.target;
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

Again, notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. According to the jQuery documentation, these are the allowed event names:
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;blur, focus, load, resize, scroll, unload, click, dblclick,  mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select,  submit, keydown, keypress, keyup, error&lt;/tt&gt;.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Most helper elements are named after the event names above, so you have things like &lt;tt&gt;click()&lt;/tt&gt;, &lt;tt&gt;focus()&lt;/tt&gt;, &lt;tt&gt;submit()&lt;/tt&gt;, etc.&lt;/p&gt;


	&lt;p&gt;Example using an anonymous function as the functioned to be executed when the event is triggered:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").click(function () { 
  // The element is access by using "$(this)" 
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

	&lt;h3&gt;Event Delegation&lt;/h3&gt;


	&lt;p&gt;One often source of confusion is how to attach event listeners to dynamically-generated &lt;span class="caps"&gt;DOM&lt;/span&gt; elements (for example, adding &lt;tt&gt;&amp;lt;li&amp;gt;&lt;/tt&gt; elements to a list via &lt;span class="caps"&gt;AJAX&lt;/span&gt;).&lt;/p&gt;


	&lt;p&gt;When an element is crated, it is not already bound to an event listener, so you will have to perform a two-step process: create the element in the &lt;span class="caps"&gt;DOM&lt;/span&gt;, and then attach the event listener. This can get tedious easily, so instead of attaching the event listener to the &lt;span class="caps"&gt;DOM&lt;/span&gt; element desired, you attach an event listener to a parent element in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree.&lt;/p&gt;


	&lt;p&gt;In turn, this parent element acts as a &amp;#8220;catch all&amp;#8221; event listener. It will figure out which element triggered the event in the first place and act accordingly. This technique is referred to as &lt;a href="http://icant.co.uk/sandbox/eventdelegation/"&gt;event delegation&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Event delegation works because some events &amp;#8220;bubble up&amp;#8221; from child nodes to parent nodes in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree; see &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling"&gt;event bubbling&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Hope thins helps someone out there getting started. Post your questions if you run into trouble. :)&lt;/p&gt;</description>
      <pubDate>Fri, 24 Oct 2008 08:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3690a8d5-8f49-4316-95b8-250cc37c4f3f</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2008/10/24/attaching-functions-to-events-in-javascript-the-unobstrusive-way-part-2</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>html</category>
      <category>DOM</category>
      <category>prototype</category>
      <category>jquery</category>
    </item>
    <item>
      <title>Slider Controls with Prototype and Scriptaculous</title>
      <description>&lt;div style="float:right;"&gt;&lt;a href="/files/Slider.zip"&gt;&lt;img src="/files/slider.gif" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;Even though web-based applications have the majority of input elements found in traditional desktop-based ones, once in a while you are going to wish you had input elements that for some reason did not make it to the &lt;span class="caps"&gt;HTML&lt;/span&gt; standards.

	&lt;p&gt;One of the input elements I wish were supported by default are draggable slider controls. There is something cool about having a slider to quickly change a setting of a search or a calculation, instead of having to type values on a text box or clicking on drop-down boxes.&lt;/p&gt;


	&lt;p&gt;Out of the myriad of options I found during a quick Google search, I found these two options to be the most attractive:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://developer.yahoo.com/yui/examples/slider/slider-ticks.html"&gt;Yahoo UI Slider Control&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://wiki.script.aculo.us/scriptaculous/show/SliderDemo"&gt;Scriptaculous Slider Control&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Since I was already using Scriptaculous on the application in question, I decided to stick to that solution. Yahoo&amp;#8217;s version is definitely worth a look though. At first glance, Yahoo&amp;#8217;s example looks better than the Scriptaculous one, but as you will see we can get the best of both worlds.&lt;/p&gt;


	&lt;p&gt;One word of warning; even though sliders are a useful control, you should exercise discretion as to not over use them. The truth of the matter is that not everyone is familiar using sliders on the web, and it can quickly become a usability problem if not careful. Good uses for sliders are things like mortgage calculators, search result filter controls, and things of that sort. Do not replace menus or tabs with a slider ;)&lt;/p&gt;


	&lt;p&gt;Onto the interesting part. In order to use Scriptculous&amp;#8217; sliders, you will need to make sure you are including the library with the slider component. By default, everything is included when you include scriptaculous in the traditional way:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
&amp;lt;script src="prototype.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="scriptaculous.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;This will load about six components, but it is a good practice to load only those components that you are actually using. In my case, I&amp;#8217;ll be using effects and slider so I&amp;#8217;m calling Scriptaculous in this fashion:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;script src="prototype.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;script src="scriptaculous.js?load=effects,slider" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

A Scriptaculous slider consist of two things:
	&lt;ul&gt;
	&lt;li&gt;A &amp;#8220;parent&amp;#8221; element (typically a &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt;) deemed to be the &amp;#8220;track&amp;#8221; on which a knob will slide.&lt;/li&gt;
		&lt;li&gt;A &amp;#8220;child&amp;#8221; nested element (typically a &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt;) that will act as the element to be slided across the length (or height) of the track.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can create a slider track and controls purely based on &lt;span class="caps"&gt;CSS&lt;/span&gt; code like in the Scriptaculous sample, but I chose to use images within &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; elements to obtain the look of the Yahoo UI slider control. A possible &lt;span class="caps"&gt;HTML&lt;/span&gt; mark-up would be:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;div id="track1" style="background-image:url(bg-fader.gif); background-repeat:no-repeat; width:209px; height:28px;"&amp;gt;
  &amp;lt;div id="handle1" style="background-image:url(thumb-n.gif); background-repeat:no-repeat; width:17px; height:21px; cursor:move;"&amp;gt; &amp;lt;/div&amp;gt;
 &amp;lt;/div&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;In the example above, we have a parent &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; element (the track) with an ID of &lt;tt&gt;track1&lt;/tt&gt; and a child (the handle) element with an ID of &lt;tt&gt;handle1&lt;/tt&gt;. In this case the width and height of the &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; elements match the track and handle image dimensions.&lt;/p&gt;


	&lt;p&gt;Here is where the magic comes. Lets make a &lt;tt&gt;Slider&lt;/tt&gt; object to give life to our &lt;span class="caps"&gt;HTML&lt;/span&gt; slider:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;script type="text/javascript" language="javascript"&amp;gt;
  // &amp;lt;![CDATA[
  window.onload = function() {
    new Control.Slider('handle1',
                       'track1',                                  
                       {axis:'horizontal',
                        range: $R(1,5)
                       });
  }
  // ]]&amp;gt;
 &amp;lt;/script&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;In the example above, we are creating a horizontal slider with a handle element ID of &lt;tt&gt;handle1&lt;/tt&gt; and a track element ID of &lt;tt&gt;track1&lt;/tt&gt;. The slider possible values range from 1 to 5.&lt;/p&gt;


	&lt;p&gt;You may have noticed that the code is within the document&amp;#8217;s &lt;tt&gt;onLoad&lt;/tt&gt; event. This is because the slider object needs to be created after the track and slider elements have completely loaded. Alternatively, you could place the JavaScript code somewhere after the &lt;span class="caps"&gt;HTML&lt;/span&gt; elements in your document, but it is typically not a good idea to place JavaScript anywhere other than in its own &lt;tt&gt;.js&lt;/tt&gt; file and then calling that file on your document. See my other post about &lt;a href="http://enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way"&gt;unobstrusive JavaScript&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;There are several options you can play with to create many variations of Scriptaculous sliders. For a good reference, check out the &lt;a href="http://wiki.script.aculo.us/scriptaculous/show/Slider"&gt;slider documentation page&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Download a complete working demo &lt;a href="/files/Slider.zip"&gt;here&lt;/a&gt;. Just unzip the file and open the &lt;tt&gt;index.html&lt;/tt&gt; file with your browser. You should see some sliders with different attributes and further examples as to how to get useful data from the sliders :)&lt;/p&gt;</description>
      <pubDate>Tue, 11 Dec 2007 07:00:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3ee3ab69-fd9b-4abc-8dfe-1698f9b3a7b1</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2007/12/11/html-slider-controls-with-prototype-and-scriptaculous</link>
      <category>Web Development</category>
      <category>html</category>
      <category>slider</category>
      <category>scriptaculous</category>
      <category>prototype</category>
      <category>DOM</category>
      <category>css</category>
      <enclosure type="application/zip" url="http://www.enriquedelgado.com/files/Slider.zip" length="284287"/>
    </item>
    <item>
      <title>Attaching functions to events in JavaScript: The unobstrusive way!</title>
      <description>&lt;p&gt;I needed to set a simple focus when a page loads in a legacy site I was working on so I decided to exercise a &amp;#8220;better way&amp;#8221; to attach functions to events in JavaScript.&lt;/p&gt;


In the past, I used to attach &lt;code&gt;onLoad&lt;/code&gt; events through an anonymous function like so:
&lt;pre style="overflow: auto; background: #EEE;"&gt;
window.onload = function() {
  init();
}

function init() {
  //do something interesting
}
&lt;/pre&gt;
This method has its drawbacks tho; it assumes that this script is the only script that may act upon a document. We &amp;#8220;assume instead that it is part of a whole group of scripts all of which fulfilling different tasks.&amp;#8221; As described in this awesome &lt;a href="http://icant.co.uk/articles/from-dhtml-to-dom/from-dhtml-to-dom-scripting.html#domasset3"&gt;webzine&lt;/a&gt; by &lt;a href="http://icant.co.uk/articles/from-dhtml-to-dom/"&gt;Chris Heilmann&lt;/a&gt; .

Instead, Chris points out a better solution; something unobstrusive (because you don&amp;#8217;t have to mix JavaScript with &lt;span class="caps"&gt;HTML&lt;/span&gt;) and that plays well with others (because more than one script can act upon a document):
&lt;pre style="overflow: auto; background: #EEE;"&gt;
  // Attach the function init() to the window.onload event:
  addEvent(window,'load',init,false);

  // Initialization; I'm setting the focus on the first field:
  function init() {
    //do something interesting
  }

  // Function to attach events to objects.
  // (http://icant.co.uk/articles/from-dhtml-to-dom/from-dhtml-to-dom-scripting.html#domasset3)
  function addEvent(elm, evType, fn, useCapture){
    if (elm.addEventListener)
    {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      return r;
    } else {
      elm['on' + evType] = fn;
    }
  }
&lt;/pre&gt;

	&lt;p&gt;The key here is the way &lt;code&gt;addEvent&lt;/code&gt; adds event listeners to the document.&lt;/p&gt;</description>
      <pubDate>Thu, 03 May 2007 10:33:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:643e3e53-254e-4719-83d6-e3d3f97b236d</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
