eBook authors eBook Software - create and publish your own ebooks
Create your own eBooks
 
   
EBookApprentice.com
Learn How To Create, Publish & Market E-Books
 
   
EBookCompiler.com
E-Book Creation Software
 
   
EBookPower.com
Add sophisticated multimedia to your E-Books
 
   
CoverFactory.com
Create E-Book covers in minutes
 
 
  EBookSubmit.com
E-Book Marketing & Promotion made easy
 
  EBookJungle.com
Search engine for E-Books
 
  EBookInterviews.com
Interviews with eBook authors
 
  EBookEnhance.com
Tools for building better eBooks
 
 
 
Archived Message:

calculate


 
fone4free All I want to do is have the ebook reader enter a couple of numbers and have the answer supplied to them in a messagebox.
I just want some clues how to start to work it out.
Activ script of can I do it with html?
DAvid

Posted on: 6:25 am on April 8, 2003
KC Here's a way in JavaScript you might be able to adapt.

If you have an HTML editor such as 1st Page 2000 (free athttp://www.evrsoft.com/download)you can paste this code into a new blank document, replace all occurances of &lt; with < and all occurances of &gt; with > and save it as test.htm you'll be able to see how it is working and modify it to suit your needs.

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Add Two Numbers&lt;/title&gt;

&lt;script language="JavaScript"&gt;

function NumOnly(event) {
&lt;!-- Make sure only numbers or a decimal point are entered //--&gt;
var charCode = (event.keyCode)
if (charCode &gt; 31 && (charCode &lt; 48 || charCode &gt; 57)) {
if (charCode !=46) {
return false;
}
}
}

function calculate() {
&lt;!-- Check if first box was left empty //--&gt;
if (document.myform.firstnumber.value == "" ) {
  &lt;!-- If so, focus on first box //--&gt;
  myform.firstnumber.focus();
  &lt;!-- and exit this function //--&gt;
  return false;
}

&lt;!-- Check if second box was left empty //--&gt;
if (document.myform.secondnumber.value == "" ) {
  &lt;!-- If so, focus on second box //--&gt;
  myform.secondnumber.focus();
  &lt;!-- and exit this function //--&gt;
  return false;
}

&lt;!-- Get first number from the form  (named 'myform') //--&gt;
var a=parseFloat(document.myform.firstnumber.value);

&lt;!-- Get second number from the form (named 'myform') //--&gt;
var b=parseFloat(document.myform.secondnumber.value);

&lt;!-- DO CALCULATION HERE - Here, a is added to b to give c //--&gt;
var c=a+b;

&lt;!-- Display some text, then the answer to the sum, then a reset button in the 'answer' span //--&gt;
document.getElementById("answer" ).innerHTML = '&lt;p&gt;'+a+' + '+b+' = '+c+'&lt;/p&gt;&lt;input type="button" value="Reset" onClick="resetform();"&gt;';
}

function resetform() {
   &lt;!-- Clear form //--&gt;
document.myform.reset();

&lt;!-- Change text in the 'answer' span //--&gt;
document.getElementById("answer" ).innerHTML = '&lt;p&gt;Enter two numbers & click &lt;b&gt;Answer&lt;/b&gt;.&lt;/p&gt;&lt;INPUT type="button" value="Answer" onClick="calculate();"&gt;';

&lt;!-- Focus on first box //--&gt;
myform.firstnumber.focus();
}

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;table width="300" cellpadding="0" cellspacing="0" border="0"&gt;
 &lt;tr&gt;
   &lt;td&gt;
     &lt;FORM name="myform"&gt;
       &lt;INPUT name="firstnumber" size="20" onkeypress="return NumOnly(event);"&gt;&lt;br&gt;&lt;br&gt;
       &lt;INPUT name="secondnumber" size="20" onkeypress="return NumOnly(event);"&gt;&lt;br&gt;&lt;br&gt;
       &lt;span id="answer"&gt;
     &lt;p&gt;Enter two numbers & click &lt;b&gt;Answer&lt;/b&gt;.&lt;/p&gt;
 &lt;INPUT type="button" value="Answer" onClick="calculate();"&gt;
   &lt;/span&gt;
 &lt;/FORM&gt;
   &lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt;


Edit:

Just incase that's hard to follow, here's a simplified version of the same thing. It won't filter unwanted characters or swap buttons and clear the fields, etc., but with all that taken out it might be clearer:

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Add Two Numbers&lt;/title&gt;

&lt;script language="JavaScript"&gt;
function calculate() {
var a=parseFloat(document.myform.firstnumber.value);
var b=parseFloat(document.myform.secondnumber.value);
var c=a+b;
document.getElementById("answer" ).innerHTML = c;
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;table width="300" cellpadding="0" cellspacing="0" border="0"&gt;
 &lt;tr&gt;
   &lt;td&gt;
     &lt;FORM name="myform"&gt;
       &lt;INPUT name="firstnumber" size="20"&gt;&lt;br&gt;&lt;br&gt;
       &lt;INPUT name="secondnumber" size="20"&gt;&lt;br&gt;&lt;br&gt;
       &lt;INPUT type="button" value="Answer" onClick="calculate();"&gt;
 &lt;/FORM&gt;
 &lt;span id="answer"&gt;&lt;/span&gt;
   &lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt;

Does that cover what you need to do?

(Edited by KC at 1:41 am on April 9, 2003)


Posted on: 4:51 pm on April 8, 2003

Go to Active Discussion Thread

Participate in Current/New Discussions

List All Archived Forums



Copyright © 2000-2015, Answers 2000 Limited.

With any business, it is up to the individual owner of said business to ensure the success of the business. You may make more or less than any sample figures or results that might be quoted on our web sites or other publications. All business involves risk, and many businesses do not succeed. Further, Answers 2000 Limited does NOT represent that any particular individual or business is typical, or that any results or experiences achieved by any particular individual/business is necessarily typical.

Disclosure: Our company's websites' content (including this website's content) includes advertisements for our own company's websites, products, and services, and for other organization's websites, products, and services. In the case of links to other organization's websites, our company may receive a payment, (1) if you purchase products or services, or (2) if you sign-up for third party offers, after following links from this website. Unless specifically otherwise stated, information about other organization's products and services, is based on information provided by that organization, the product/service vendor, and/or publicly available information - and should not be taken to mean that we have used the product/service in question. Additionally, our company's websites contain some adverts which we are paid to display, but whose content is not selected by us, such as Google AdSense ads. For more detailed information, please see Advertising/Endorsements Disclosures

Our sites use cookies, some of which may already be set on your computer. Use of our site constitutes consent for this. For details, please see Privacy.

Contact Us    About and Terms Of Use    Privacy    Advertising/Endorsements Disclosures