
<?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>Mental Pandiculation &#187; javascript</title>
	<atom:link href="http://mentalpandiculation.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://mentalpandiculation.com</link>
	<description>One Man's Attempt To Find Elegant Code Through Big Words</description>
	<lastBuildDate>Mon, 27 Jun 2011 23:31:03 +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>Firefox Styling Gotcha With ComponentArt TreeView</title>
		<link>http://mentalpandiculation.com/2010/04/firefox-styling-gotcha-with-componentart-treeview/</link>
		<comments>http://mentalpandiculation.com/2010/04/firefox-styling-gotcha-with-componentart-treeview/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 17:55:28 +0000</pubDate>
		<dc:creator>Niklaus Wirth's Ghost</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[hiding controls]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Safari]]></category>

		<guid isPermaLink="false">http://mentalpandiculation.com/?p=259</guid>
		<description><![CDATA[This was a fun one that I ran into today. I don&#8217;t think it&#8217;s specific to ComponentArt but that&#8217;s where I ran into it. We have a web form that has a couple of tables on it, used for switching back and forth between viewable controls. One of the tables contains a ComponentArt TreeView. Based [...]]]></description>
			<content:encoded><![CDATA[<p>This was a fun one that I ran into today.  I don&#8217;t think it&#8217;s specific to ComponentArt but that&#8217;s where I ran into it.  We have a web form that has a couple of tables on it, used for switching back and forth between viewable controls.  One of the tables contains a ComponentArt TreeView.  Based on user interaction, we would use JavaScript to change the applicable table&#8217;s styles to block or none, depending on whether we wanted it shown or not.  This worked great in all browsers but Firefox.  Firefox seemed to ignore all sizing on the table and instead would fit the table to the TreeView based on the contents.  We wanted the tables to stay 100% sized so having weird rendering problems in Firefox wasn&#8217;t acceptable.</p>
<p>Turns out, Firefox doesn&#8217;t like having tables&#8217; styles set to &#8220;block&#8221; as a way to make them visible, ala the 10th post in <a href="http://www.componentart.com/community/forums/p/6355/6355.aspx">this thread</a>.  So instead of doing this:</p>
<pre name="code" class="jscript">    function showCourseList()
		{
			document.getElementById('tblList').style.display = 'block';
			document.getElementById('tblTree').style.display = 'none';
			fixMoz();
		}</pre>
<p>I did this instead:</p>
<pre name="code" class="jscript">    function showCourseList()
		{
			document.getElementById('tblList').style.display = '';
			document.getElementById('tblTree').style.display = 'none';
			fixMoz();
		}</pre>
<p>This works not only in Firefox but all other major tested browsers which in my case is IE and Safari.  </p>
]]></content:encoded>
			<wfw:commentRss>http://mentalpandiculation.com/2010/04/firefox-styling-gotcha-with-componentart-treeview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making CheckBoxList Play Nice With JavaScript</title>
		<link>http://mentalpandiculation.com/2010/03/making-checkboxlist-play-nice-with-javascript/</link>
		<comments>http://mentalpandiculation.com/2010/03/making-checkboxlist-play-nice-with-javascript/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 14:55:43 +0000</pubDate>
		<dc:creator>Niklaus Wirth's Ghost</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://mentalpandiculation.com/?p=251</guid>
		<description><![CDATA[I&#8217;m working on a web page that allows the user to apply a variety of filters to a set of data. The old implementation involved a check box for each filter and then a ListBox with MultipleSelection set to true. The user would tick what ever filter they wanted to include and then select the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a web page that allows the user to apply a variety of filters to a set of data.  The old implementation involved a check box for each filter and then a ListBox with MultipleSelection set to true.  The user would tick what ever filter they wanted to include and then select the items out of the ListBox.  Every time the user makes a selection in one of the ListBoxes, we&#8217;re making an AJAX call to update the current filters.  That works great but the user has to do two things, first tell us that they want to use a particular filter using the appropriate CheckBox and then select items out of the ListBox, sometimes holding down the Shift or Control key to do multiple selection.  </p>
<p>We decided to change that to a CheckBoxList for each filter.  This removes one step and makes the UI a little more fluid.  However, it did cause one problem.  Previously, the ListBoxes were being loaded with data on PageLoad by just adding a new ListItem for each piece of data.  This works well because the ListItem can hold whatever value you want to key off of.  However, a CheckBoxList renders as a table of CheckBoxes, none of which have the ability to hold a piece of data to key off of.  </p>
<pre name="code" class="csharp">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            for(int x = 0; x < 3; x++)
            {
                MyListBox.Items.Add(new ListItem("my value is " + x, x.ToString()));
            }
        }
    }
</pre>
<p>That code rendered a ListBox that had source like this:</p>
<pre name="code" class="html">
<select size="4" name="Tabstrip1:StudentsTab:MyListBox" id="Tabstrip1_StudentsTab_MyListBox" OnClick="DoSomething();">
<option value="0">my value is 0</option>
<option value="1">my value is 1</option>
<option value="2">my value is 2</option>
</select>
</pre>
<p>From there, it was really easy to grab the value of any selections via JavaScript.  Unfortunately, as mentioned above, the CheckBoxList does not render that way.  Using the same code in the PageLoad ends up rendering this HTML:</p>
<pre name="code" class="html">
<table id="Tabstrip1_StudentsTab_MyCheckBoxList" OnClick="DoSomething();" border="0">
<tr>
<td>
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_0" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:0" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_0">my value is 0</label></td>
</tr>
<tr>
<td>
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_1" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:1" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_1">my value is 1</label></td>
</tr>
<tr>
<td>
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_2" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:2" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_2">my value is 2</label></td>
</tr>
</table>
</pre>
<p>As you can see, none of the checkboxes have the value attribute.  There is now a label for each one but it has the Key of the ListItem and not the Value which isn't particularly helpful when you want to display one piece of data for the user but process another one based on their selection.  What to do?</p>
<p>As it turns out, you can add an attribute that gets rendered as a span around both the input and label elements.  In JavaScript, you can then grab an array of the inputs and an array of the span elements.  When one of the input boxes is found to be checked, you can get the corresponding span element's attribute and use it.  It looks a little something like this:</p>
<pre name="code" class="csharp">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            for(int x = 0; x < 3; x++)
            {
                ListItem li = new ListItem {Text = "my value is " + x, Value = x.ToString()};
                li.Attributes.Add("myspecialid", x.ToString());
                MyCheckBoxList.Items.Add(li);
            }
        }
    }
</pre>
<p>When the items are loaded into the ListItems, we add an attribute called "myspecialid".  It can be anything you want though it doesn't work if you pick a known attribute like id or value.  Once that's done, the HTML renders like this:</p>
<pre name="code" class="html">
<table id="Tabstrip1_StudentsTab_MyCheckBoxList" OnClick="DoSomething();" border="0">
<tr>
<td><span myspecialid="0">
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_0" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:0" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_0">my value is 0</label></span></td>
</tr>
<tr>
<td><span myspecialid="1">
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_1" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:1" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_1">my value is 1</label></span></td>
</tr>
<tr>
<td><span myspecialid="2">
<input id="Tabstrip1_StudentsTab_MyCheckBoxList_2" type="checkbox" name="Tabstrip1:StudentsTab:MyCheckBoxList:2" /><label for="Tabstrip1_StudentsTab_MyCheckBoxList_2">my value is 2</label></span></td>
</tr>
</table>
</pre>
<p>Our new attribute has been included in a span tag around the input element and the value we want to track is included there.  Now in JavaScript, we can do this:</p>
<pre name="code" class="javascript">
function DoSomething() {
    var tableBody = document.getElementById("Tabstrip1_StudentsTab_MyCheckBoxList");
    var inputArray = tableBody.getElementsByTagName("input");
    var spanArray = tableBody.getElementsByTagName("span");
    for (var x = 0; x < inputArray.length; x++) {
        if (inputArray[x].checked) {
            alert("The id I want is " + spanArray[x].getAttribute("myspecialid"));
        }
    }
    if (groupsSelected.length > 0) {
        __aspx.HandleMonitorGroupFilter(groupsSelected);
    }
}
</pre>
<p>We grab all the input and span elements in the table.  Then when we find a checkbox that is checked, we can grab the attribute we're interested in off of the appropriate span element using the getAttribute method.  This is slightly hackish in that the two arrays of elements have to be in the same order but that seems like a reasonably safe assumption to make.</p>
<p>Overall, it seems like a good way to make the UI more fluid while still retaining the ability to manipulate necessary data on the AJAX calls.  </p>
]]></content:encoded>
			<wfw:commentRss>http://mentalpandiculation.com/2010/03/making-checkboxlist-play-nice-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JsTestDriver First Impressions</title>
		<link>http://mentalpandiculation.com/2010/01/jstestdriver-first-impressions/</link>
		<comments>http://mentalpandiculation.com/2010/01/jstestdriver-first-impressions/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:22:11 +0000</pubDate>
		<dc:creator>Niklaus Wirth's Ghost</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JsTestDriver]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://mentalpandiculation.com/?p=235</guid>
		<description><![CDATA[I&#8217;m currently trying to get some unit tests written around a bunch of JavaScript code and I&#8217;m using JsTestDriver to do it. So far, I&#8217;m pretty impressed though there are some little gotchas here and there. It&#8217;s been a really good framework for basic testing assuming you aren&#8217;t doing a ton of work with other [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently trying to get some unit tests written around a bunch of JavaScript code and I&#8217;m using <a href="http://code.google.com/p/js-test-driver/">JsTestDriver</a> to do it.  So far, I&#8217;m pretty impressed though there are some little gotchas here and there.  It&#8217;s been a really good framework for basic testing assuming you aren&#8217;t doing a ton of work with other libraries like Prototype.js and <a href="http://script.aculo.us/">script.aculo.us</a> which of course I am.  I&#8217;m sure there are ways around it but it&#8217;s going to take some digging to figure out what they are.</p>
<p>One minor thing that has been different for me from the documentation is that when I try to run tests, I need to specify the server explicitly on the command line even though I have a server running and a browser captured.  The documentation says you can do this:</p>
<pre name="code" class="java">java -jar JsTestDriver.jar --tests all</pre>
<p>.</p>
<p>However, I&#8217;ve found that I need to do:</p>
<pre name="code" class="java">java -jar JsTestDriver.jar --tests all --server http://localhost:9876</pre>
<p>Where the server value may be different according to what server you started up.</p>
<p>Now I&#8217;m off to figure out how I can mock some of the Prototype.js dependencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://mentalpandiculation.com/2010/01/jstestdriver-first-impressions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

