<?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>Joe Lanman &#187; jquery</title>
	<atom:link href="http://www.joelanman.com/by_tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joelanman.com</link>
	<description>Web design and development when not playing computer games</description>
	<lastBuildDate>Sat, 18 Dec 2010 19:10:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Usable multiple-selection with checkbox lists and jQuery</title>
		<link>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/</link>
		<comments>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 19:00:51 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[User Experience]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=11</guid>
		<description><![CDATA[There are two out-of-the-box methods for multiple selection in HTML: checkboxes and the &#60;select&#62; tag. Neither are perfect &#8211; I&#8217;ll be discussing why, and proposing a sort of mashup of the best aspects of both, using jQuery. The vanilla HTML implementations of these two methods are shown below: 1) Select tag: Bad Off the Wall [...]]]></description>
			<content:encoded><![CDATA[<p>There are two out-of-the-box methods for multiple selection in HTML: checkboxes and the &lt;select&gt; tag. Neither are perfect &#8211; I&#8217;ll be discussing why, and proposing a sort of mashup of the best aspects of both, using jQuery.</p>
<p><span id="more-11"></span></p>
<p>The vanilla HTML implementations of these two methods are shown below:</p>
<p><strong>1) Select tag:</strong></p>
<select multiple="multiple" size="3">
<option>Bad</option>
<option>Off the Wall</option>
<option>Thriller</option>
</select>
<p>(Hold ctrl or command to select multiple values)</p>
<p><strong>2) Checkboxes and Label tag:</strong></p>
<div>
<input id="check_a" type="checkbox" /><label for="check_a">Bad</label></div>
<div>
<input id="check_b" type="checkbox" /><label for="check_b">Off the Wall</label></div>
<div>
<input id="check_c" type="checkbox" /><label for="check_c">Thriller</label></div>
<p>Note that the &lt;label&gt; tag allows users to click the label to toggle the checkbox on and off.</p>
<p>In terms of usability,  the checkbox method is more straightforward &#8211; it is obvious how to select multiple values, whereas with the select method we have to tell the user which keys to hold down. In addition to that, if a user has selected multiple items, and then forgets to hold the correct key on the next click, their selection is lost and only the last value clicked is selected. On a list that can be scrolled, it may not even be apparent that the other selections have been lost.</p>
<p>The disadvantages of the checkbox method are:</p>
<ul>
<li>It&#8217;s not obvious that the label can be clicked.</li>
<li>It would be nice if a larger area were clickable, as with the SELECT example</li>
<li>It would be nice if the entire row were highlighted, as with the SELECT example</li>
<li>The values are not scrollable &#8211; adding more will simply show a longer list</li>
</ul>
<p>We&#8217;re going to fix all that with some simple CSS and Javascript (using JQuery).</p>
<p><a href="/static/examples/multiple_selection">Click to see an example page</a></p>
<p><strong>Edit 1 &#8211; 9/4/09:</strong> Simplify code as per Victor&#8217;s comments, and fix refresh issue in Firefox</p>
<p><strong>Edit 2 &#8211; 9/4/09: </strong>Simplified again, using just the label tags, without &lt;a&gt; tags, from the <a href="http://www.reddit.com/r/web_design/comments/8b876/my_blog_post_on_usable_multipleselection_any/" class="aga aga_1">reddit comments</a></p>
<pre><code>
	$(document).ready(function() {

		// make sure labels are drawn in the correct state

		$('label').each(function()
		{

			if ($(this).find(':checkbox').attr('checked'))
				$(this).addClass('selected');

		});

		// toggle label css when checkbox is clicked

		$(':checkbox').click(function(e)
		{

			var checked = $(this).attr('checked');
			$(this).closest('label').toggleClass('selected', checked);

		});

	});
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using JQuery and Javascript to parse XML to objects</title>
		<link>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/</link>
		<comments>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 00:23:34 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=3</guid>
		<description><![CDATA[Right &#8211; thought I&#8217;d post some actual code as proof of actual development. I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be checked for dangerous syntax, and then eval-ed and it becomes [...]]]></description>
			<content:encoded><![CDATA[<p>Right &#8211; thought I&#8217;d post some actual code as proof of actual development.</p>
<p>I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be <a href="http://docs.mootools.net/Utilities/JSON" class="aga aga_5">checked for dangerous syntax</a>, and then eval-ed and it becomes Javascript objects ready to use. The <a href="http://www.youtube.com/watch?v=IE9ripDJHng&amp;feature=related" class="aga aga_6">pot-noodle</a> of data formats.</p>
<p><span id="more-3"></span></p>
<p>Pot noodles are all very well but my friend <a href="http://www.x13n.com/" class="aga aga_7">Alex</a> prefers XML. It&#8217;s much more readable, and pretty much every language and its dog can parse it. I wrote a function to turn my Python data into XML which I&#8217;ll post later, but this post is about my Javascript XML parser, as it was harder to get right.</p>
<p>In summary the parser function takes a complex heirarchical XML tree with nodes and attributes, and turns it into equivalent JS objects and properties. The full code as it stands is at the end of the post, I&#8217;ll go through the main steps here:</p>
<ol>
<li>The function accepts the first child of the XML file and a parent object as arguments.</li>
<li>If the node name ends with &#8216;_array&#8217; then it is, unsurprisingly, an array. This is a naming convention, but I couldn&#8217;t see how else to make sure arrays were consistently dealt with.</li>
<li>If the node has no attributes of its own, treat it as an attribute itself. Again, this is a weird convention on my part, so I can use nodes to store large sections of text, instead of putting them in XML attributes, which would be hard to read.</li>
<li>Check whether the object can be pushed into a parent array, if that fails the parent must be an object &#8211; append the the new object to the parent.</li>
<li>Loop the attributes and assign them as properties to the object.</li>
<li>Loop the child nodes and send them to the function with the current array/object as parent.</li>
</ol>
<p><strong>Javascript code:</strong></p>
<pre>
<code>
xmlToObj : function(xml,parent_obj) {

	if (parent_obj == null)
	{

		// base object

		var new_obj = {};
		xml = xml.firstChild;

	} else if (xml.nodeName.search(/_array$/) != -1) {

		// array

		var new_obj = parent_obj[xml.nodeName] = [];

	} else if(xml.attributes.length == 0) {

		// node which is really a string attribute

		try {

			parent_obj[xml.nodeName] = xml.firstChild.nodeValue;

		} catch(error) {

			parent_obj[xml.nodeName] = '';

		}

	} else {

		if (parent_obj instanceof Array) {

			//member of an array

			var new_obj = {};
			parent_obj[$(xml).attr('id')] = new_obj;

		} else {

			// new object

			var new_obj = parent_obj[xml.nodeName] = {};

		}				

	}

	// map xml attributes to object properties

	$(xml.attributes).each(function()
	{
		if (this.nodeValue.toLowerCase() == 'true') {

			new_obj[this.nodeName] = true;

		} else if (this.nodeValue.toLowerCase() == 'false') {

			new_obj[this.nodeName] = false;

		} else {

			new_obj[this.nodeName] = parseInt(this.nodeValue);

		}
	});

	if ($(xml).children().length &gt; 0)
	{

		$(xml).children().each(function()
		{
			page_edit.xmlToObj(this, new_obj);

		});

	}

	return new_obj;

}</code></pre>
<p><strong>XML data</strong></p>
<pre>
<code>
&lt;page active="True" id="1" is_template="False" position="1" template_id="1" visible="True"&gt;
	&lt;title&gt;
		project
	&lt;/title&gt;
	&lt;sections_array&gt;
		&lt;page_section id="1" page_id="1" position="0" section_id="1" visible="True"&gt;
			&lt;section active="True" attribs="None" id="1"&gt;
				&lt;elements_array&gt;
					&lt;section_element element_id="1" id="1" position="0" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" id="1"&gt;
							&lt;content&gt;

								test
							&lt;/content&gt;
							&lt;title&gt;
								title
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
					&lt;section_element element_id="2" id="2" position="1" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" content="None" id="2"&gt;
							&lt;title&gt;
								test
							&lt;/title&gt;

						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;
				&lt;title&gt;
					test_section
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
		&lt;page_section id="2" page_id="1" position="1" section_id="2" visible="True"&gt;
			&lt;section active="True" attribs="None" id="2"&gt;

				&lt;elements_array&gt;
					&lt;section_element element_id="4" id="4" position="0" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="4"&gt;
							&lt;content&gt;
								Hello my name is Joe
							&lt;/content&gt;
							&lt;title&gt;
								Fred
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;

					&lt;section_element element_id="3" id="3" position="1" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="3"&gt;
							&lt;content&gt;
								some content
							&lt;/content&gt;
							&lt;title&gt;
								Feedy doo da
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;

				&lt;title&gt;
					arab
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
	&lt;/sections_array&gt;
&lt;/page&gt;
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.441 seconds -->

