<?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>Random Snippets &#187; input button</title>
	<atom:link href="http://www.randomsnippets.com/tag/input-button/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to dynamically add form elements via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:43 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[appendchild]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[div element]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic text]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[input button]]></category>
		<category><![CDATA[input text]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[text element]]></category>
		<category><![CDATA[text input]]></category>
		<category><![CDATA[text inputs]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/</guid>
		<description><![CDATA[

Not all forms are meant to be static.  Sometimes, you want to allow the users to add certain parts of the form as they need them.  Here is a nice example of dynamically adding inputs to your form as users need them.  In addition, an input limit has been implemented in the [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/addInput.js" language="Javascript" type="text/javascript"></script><br />
<script src="/wp-includes/js/addAllInputs.js" language="Javascript" type="text/javascript"></script><br />
Not all forms are meant to be static.  Sometimes, you want to allow the users to add certain parts of the form as they need them.  Here is a nice example of dynamically adding inputs to your form as users need them.  In addition, an input limit has been implemented in the script and it is set to 3.</p>
<fieldset>
<legend>Demo</legend>
<form method="POST">
<div id="dynamicInput">
          Entry 1<br />
<input type="text" name="myInputs[]">
     </div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
</fieldset>
<p><span id="more-6"></span><br />
Here is the plain HTML for the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;/wp-includes/js/addInput.js&quot; language=&quot;Javascript&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;form method=&quot;POST&quot;&gt;
     &lt;div id=&quot;dynamicInput&quot;&gt;
          Entry 1&lt;br&gt;&lt;input type=&quot;text&quot; name=&quot;myInputs[]&quot;&gt;
     &lt;/div&gt;
     &lt;input type=&quot;button&quot; value=&quot;Add another text input&quot; onClick=&quot;addInput('dynamicInput');&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>When the user clicks on the <b>Add another text input</b> button, the <code>addInput</code> JavaScript function is executed and given the <b>dynamicInput</b> argument which is the name of the div that contains all our text inputs.  Here is what our JavaScript function looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span>;
<span style="color: #003366; font-weight: bold;">var</span> limit <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span>;
<span style="color: #003366; font-weight: bold;">function</span> addInput<span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">==</span> limit<span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You have reached the limit of adding &quot;</span> <span style="color: #339933;">+</span> counter <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; inputs&quot;</span><span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> newdiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>;
          newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='text' name='myInputs[]'&gt;&quot;</span>;
          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newdiv<span style="color: #009900;">&#41;</span>;
          counter++;
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are a couple of global variables:</p>
<ul>
<li><code>counter</code> &#8211; Tracks the number inputs being added.</li>
<li><code>limit</code> &#8211; The maximum number inputs allowed for the user to add. In this case, we are limiting the user to a maximum of 3 input texts that they can submit with the form.</li>
</ul>
<p>The <code>addInput</code> JavaScript function expects a parameter, which in this case is the id of the div, to add the dynamic text inputs.  Once executed, the function will first check to see if the maximum number of input texts have already been used.  If so, an alert will be given stating that the limit has been reached.</p>
<p>If the limit has not been reached, then the <code>else</code> block of the code is executed.  A new <code>div</code> element is created and the inner HTML is set with 2 things:</p>
<ul>
<li>Entry # &#8211; The global variable, <code>counter</code>, keeps track of the current number of text inputs that are in the form.</li>
<li>The input text element &#8211; The name of these text inputs,<code>myInputs[]</code>, is followed by square brackets to indicate that it is an array.  This will be more important later on when the form is submitted using the <code>POST</code> method.</li>
</ul>
<p>If the form gets submitted to a PHP file, you can easily access each input value for your <code>myInputs</code> array.  Here is the PHP code for doing just that:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$myInputs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myInputs&quot;</span><span style="color: #009900;">&#93;</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myInputs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$eachInput</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #990000;">echo</span> <span style="color: #000088;">$eachInput</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is just a sample code to <code>echo</code> all the text input values that were sent from the form.  An array variable, <code>$myInputsPHP</code>, is initialized with the <code>myInputs</code> from the form.  Then, the <code>foreach</code> loop will go through each value and print them out.</p>
<p>Here is another version of this script that adds different types of inputs such as <b>text fields</b>, <b>radio buttons</b>, <b>checkboxes</b>, and <b>textareas</b>.  To simplify things, the limit functionality has been removed.</p>
<fieldset>
<legend>Demo</legend>
<form name="myForm" method="POST">
<div id="dynamicInputs">
          Let&#8217;s start adding some inputs of different types.
     </div>
<select name='inputSelect'>
          <option value ="text">text field</option><br />
          <option value ="radio">radio button</option><br />
          <option value ="checkbox">checkbox</option><br />
          <option value ="textarea">textarea</option><br />
     </select>
<input type="button" value="Add selected input" onClick="addAllInputs('dynamicInputs', document.myForm.inputSelect.value);">
</form>
</fieldset>
<p>Here is the plain JavaScript code for the <b>addAllInputs</b> function.  It accepts 2 arguments:</p>
<ul>
<li><b>div id</b> &#8211; This is the div id where all the dynamic inputs will be added to.</li>
<li><b>input type</b> &#8211; This is the type of input that we want to add.</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counterText <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterRadioButton <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterCheckBox <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">var</span> counterTextArea <span style="color: #339933;">=</span> 0;
<span style="color: #003366; font-weight: bold;">function</span> addAllInputs<span style="color: #009900;">&#40;</span>divName<span style="color: #339933;">,</span> inputType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> newdiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>;
     <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>inputType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'text'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterText <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='text' name='myInputs[]'&gt;&quot;</span>;
               counterText++;
               <span style="color: #000066; font-weight: bold;">break</span>;
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'radio'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterRadioButton <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='radio' name='myRadioButtons[]'&gt;&quot;</span>;
               counterRadioButton++;
               <span style="color: #000066; font-weight: bold;">break</span>;
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'checkbox'</span><span style="color: #339933;">:</span>
               newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterCheckBox <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;input type='checkbox' name='myCheckBoxes[]'&gt;&quot;</span>;
               counterCheckBox++;
               <span style="color: #000066; font-weight: bold;">break</span>;
          <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'textarea'</span><span style="color: #339933;">:</span>
	       newdiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Entry &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counterTextArea <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &lt;br&gt;&lt;textarea name='myTextAreas[]'&gt;type here...&lt;/textarea&gt;&quot;</span>;
               counterTextArea++;
               <span style="color: #000066; font-weight: bold;">break</span>;
          <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newdiv<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
	</channel>
</rss>
