<?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>Anay Kamat's Weblog &#187; forms</title>
	<atom:link href="http://anaykamat.com/tag/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://anaykamat.com</link>
	<description>Technology, Programming, Career, Fun, Friends And Thoughts</description>
	<lastBuildDate>Tue, 08 May 2012 22:00:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Binding HTML Form Fields To Javascript Objects</title>
		<link>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/</link>
		<comments>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/#comments</comments>
		<pubDate>Thu, 22 May 2008 15:42:58 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[data bindings]]></category>
		<category><![CDATA[databindings]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=50</guid>
		<description><![CDATA[Consider that I have a object called &#8216;person&#8217; with a property called &#8216;Name&#8217; which returns me the name of a particular person. I want to assign this object property to a form element such that: The form element will display the value of object property (In our case, the value of person.Name) If I change [...]]]></description>
			<content:encoded><![CDATA[<p>Consider that I have a object called &#8216;person&#8217; with a property called &#8216;Name&#8217; which returns me the name of a particular person. I want to assign this object property to a form element such that:</p>
<ol>
<li>The form element will display the value of object property (In our case, the value of person.Name)</li>
<li>If I change the value in form, then it should automatically get assigned to the object property</li>
</ol>
<p>In C#, it could be done by using the following piece of code:</p>
<pre class="brush: csharp;">nameTextBox.DataBindings.Add(&quot;Text&quot;, person, &quot;Name&quot;);</pre>
<p>I really like the way DataBinding works and would really like have something like this when I&#8217;m working on web applications to bind a object property to input elements in HTML forms. So I decided to write a small javascript that would allow me to bind object properties to form elements. For example, I wanted to write something like this:</p>
<pre class="brush: xml;">&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;firstName&quot; /&gt;</pre>
<p>Thanks to the power of Javascript and prototype library, I was able to use data binding in html forms using code:</p>
<pre class="brush: jscript;">
function initializeFormBinding()
{
	var formElements = document.getElementsByTagName('input');
	$A(formElements).each(function(formElement)
				{
					Element.extend(formElement);
					initializeFormElement(formElement);
				});
}

function initializeFormElement(formElement)
{
	if (!(formElementHasObjectAttribute(formElement) &amp;amp;&amp;amp; formElementHasPropertyAttribute(formElement)))
		return;
	var objectName = getAttributeValue(formElement,'object');
	var propertyName = getAttributeValue(formElement,'property');
	window.eval('formElement.value = '+objectName+'.'+propertyName);
	window.eval('formElement.onchange=function(){ '+objectName+'.'+propertyName+' = formElement.value; }');
}

function formElementHasObjectAttribute(formElement)
{
	return getAttributeValue(formElement,'object')!=null;
}

function formElementHasPropertyAttribute(formElement)
{
	return getAttributeValue(formElement,'property')!=null;
}

function getAttributeValue(element, attributeName)
{
	return element.readAttribute(attributeName);
}
</pre>
<p>Lets see how to use this: First, create a javascript object. For example:</p>
<pre class="brush: jscript;">
var person = {};
person.firstName = '';
person.lastName = '';
person.age = '';
person.country = '';
</pre>
<p>After that call initializeFormBinding() function using onLoad attribute of body tag. For example:</p>
<pre class="brush: xml;">
&lt;body onLoad=&quot;javascript:initializeFormBinding();&quot;&gt;
</pre>
<p>Now, in each of your input elements, put the name of the object in &#8216;object&#8217; attribute and name of property in &#8216;property&#8217; attribute as follows:</p>
<pre class="brush: xml;">
&lt;form&gt;
First Name:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;firstName&quot; /&gt;&lt;br&gt;
Last Name:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;lastName&quot; /&gt;&lt;br&gt;
Age:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;age&quot; /&gt;&lt;br&gt;
Country:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;country&quot; /&gt;&lt;br&gt;
&lt;/form&gt;
</pre>
<p>Thats it&#8230;. If you initialize properties of person object, you would see those values in respective form elements. On the other hand, if you modify the value in a particular input element then it would get reflected in the corresponding object property. Anybody is free to use this code in their applications.</p>
<p><a title="Application example" href="http://www.anaykamat.com/fileuploads/JavascriptDataBinding.zip">Download a small example application.</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

