<?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; multiplication</title>
	<atom:link href="http://www.randomsnippets.com/tag/multiplication/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code for Web Developers</description>
	<lastBuildDate>Fri, 25 Nov 2011 23:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How to create your own customized calculator via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 01:01:51 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[addition and subtraction]]></category>
		<category><![CDATA[calculator]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[input buttons]]></category>
		<category><![CDATA[input object]]></category>
		<category><![CDATA[javascript code]]></category>
		<category><![CDATA[multiplication]]></category>
		<category><![CDATA[onclick event]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=15</guid>
		<description><![CDATA[This is a simple example of implementing a calculator with JavaScript and HTML. This calculator will only have the division, multiplication, addition, and subtraction operators but you can easily tack on more functions if needed. Demo Here is the html &#8230; <a href="http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script lang="javascript">
function calculate(equation) {
     var answer = eval(equation);
     document.getElementById('screen').value = answer;
}
function pushButton(buttonValue) {
     if (buttonValue == 'C') {
          document.getElementById('screen').value = '';
     }
     else {
          document.getElementById('screen').value += buttonValue;
     }
}
</script></p>
<style type="text/css">
table.calc {
     border: 2px solid #0034D1;
     background-color: #809FFF;
}
input.calc {
     width: 100%;
     margin: 5px;
}
tr.calc {
     border: 0px;
}
</style>
<p>This is a simple example of implementing a calculator with JavaScript and HTML.  This calculator will only have the division, multiplication, addition, and subtraction operators but you can easily tack on more functions if needed.</p>
<fieldset>
<legend>Demo</legend>
<table class="calc" cellpadding=4>
<tr class="calc">
<td colspan=3>
<input class="calc" id="screen" type="text"></td>
</tr>
<tr class="calc">
<td>
<input class="calc" type="button" value=1 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=2 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=3 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='/' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td>
<input class="calc" type="button" value=4 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=5 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=6 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='*' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td>
<input class="calc" type="button" value=7 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=8 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value=9 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='-' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td>
<input class="calc" type="button" value=0 onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='.' onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='C' onclick="pushButton(this.value);"></td>
<td>
<input class="calc" type="button" value='+' onclick="pushButton(this.value);"></td>
</tr>
<tr>
<td colspan=3>
<input class="calc" type="button" value='=' onclick="calculate(document.getElementById('screen').value);"></td>
</tr>
</table>
</fieldset>
<p><span id="more-15"></span><br />
Here is the html code including the CSS stuff:</p>

<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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot;&gt;
table.calc {
     border: 2px solid #0034D1;
     background-color: #809FFF;
}
input.calc {
     width: 100%;
     margin: 5px;
}
&lt;/style&gt;
&lt;fieldset&gt;
&lt;legend&gt;Demo&lt;/legend&gt;
&lt;table class=&quot;calc&quot; cellpadding=4&gt;
&lt;tr&gt;&lt;td colspan=3&gt;&lt;input class=&quot;calc&quot; id=&quot;screen&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=1 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=2 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=3 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='/' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=4 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=5 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=6 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='*' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=7 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=8 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=9 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='-' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value=0 onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='.' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='C' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='+' onclick=&quot;pushButton(this.value);&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=3&gt;&lt;input class=&quot;calc&quot; type=&quot;button&quot; value='=' onclick=&quot;calculate(document.getElementById('screen').value);&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</pre></td></tr></table></div>

<p>Most of the HTML is coding for all the input buttons and their associated <b>onclick</b> event handlers.  All the buttons, except for the <b>=</b> button, will call the <b>pushButton</b> JavaScript when clicked and passes it&#8217;s associated value.  The <b>=</b> button calls the <b>calculate</b> JavaScript function and passes the value that is in the text input which represents the string of numbers and operators that were pushed.  The rest of the HTML is just icing on the cake so that it actually looks like a calculator.</p>
<p>Here is the JavaScript code for the 2 functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> pushButton<span style="color: #009900;">&#40;</span>buttonValue<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>buttonValue <span style="color: #339933;">==</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">+=</span> buttonValue<span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> calculate<span style="color: #009900;">&#40;</span>equation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> answer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>equation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'screen'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> answer<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <b>pushButton</b> function does 2 things for us.  If the user clicks on the <b>C</b> button or the clear button, it accesses the text input object, which represents our calculator screen, via the DOM .  Once it is accessed, the value inside the text input is set to &#8221; (2 single quotes) which is an empty string.  This will basically remove whatever string is already inside the text input.  If the user clicks on any other button, the function will access the text input object and append the value that was passed over to whatever is already in our text input.  This allows us to build our string with numbers and operators.</p>
<p>The <b>calculate</b> function does exactly what it&#8217;s name suggests.  It takes our string of numbers and operators and evaluates it in line 10.  The <b>eval()</b> function treats the string as if it were JavaScript code and executes it and returns the results.  The function will then access the text input object and replace whatever the current value is with the answer from the <b>eval()</b> function.</p>
<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/26/how-to-create-your-own-customized-calculator-via-javascript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

