Hi
I have the following code and it works fine if i want the total to be in a form field but i want to write the amount to the webpage using document.write, have tried all sorts but cant get it to work - <script type="text/javascript">
-
function calculate(f)
-
{
-
var nums = f.num;
-
var ntext = f.num;
-
var result = 0;
-
for(var i=0;i<nums.length;i++)
-
{
-
if(nums[i].checked)
-
{
-
result+=parseFloat(ntext[i].value);
-
}
-
}
-
f.answer.value=Number(result).toFixed(2);
-
}
-
</script>
can anyone help???
15 2577
don’t use document.write(), use .appendChild() or .innerHTML (…).
Hi
Thanks for the advice, i am not really that good with javascript, better with asp but that wont help, can anyone either help with some code or point me to somewhere to read about it, thanks
if anyone would know, what you’re up to, there sure is some help possible. (the function does not explain much)
Sorry about that....
I am making a page that has a form with multiple checkboxes on it and when you check the box it adds the amount set to that box to a total, thats what this code does, what i want to do is be able to send that total to another page so that i can seperate the list of items in the form out into seperate types
update the total - // box refers to a representive checkbox
-
-
var total = 0;
-
-
function addAmount()
-
{
-
if (this.checked)
-
{ // if the box is checked, add its value
-
total += parseFloat(this.value);
-
}
-
else
-
{ // else subtract it
-
total -= parseFloat(this.value);
-
}
-
}
-
-
// for the sake of simplicity I use 1 checkbox and the standard functions
-
// repeat with every checkbox
- box.addEventListener("click", addAmount, false);
is this not the same as i first put on this thread just slightly different, surely my original post holds the amount i want in the variable result, i just cant get it to write to a page, i had it working but it refreshed the page and just gave me the total, i want to change some text on the page so that it changes as you click on the checkboxes
using this code - <script type="text/javascript">
-
function calculate(f)
-
{
-
{
-
var nums = f.num;
-
var ntext = f.num;
-
var result = 0;
-
for(var i=0;i<nums.length;i++)
-
{
-
if(nums[i].checked)
-
{
-
result+=parseFloat(ntext[i].value);
-
}
-
}
-
f.answer.value=Number(result).toFixed(2);
-
}
-
document.write(result);
-
}
-
</script>
@colinod
slight, but crucial differences @colinod
If I knew, where (in terms of HTML) you want to output it, I could have thought of that earlier.
for now lets assume the simple case it were a text field. - var total = 0;
-
var out = document.getElementById("total");
-
-
function addAmount()
-
{
-
if (this.checked)
-
{ // if the box is checked, add its value
-
total += parseFloat(this.value);
-
}
-
else
-
{ // else subtract it
-
total -= parseFloat(this.value);
-
}
-
out.value = total;
-
}
-
// event handling as above
edit: change the event type to "change"
Hi
Thanks but sorry to be a pain, i just cant see how this is different for getting the total in a variable, the page for all this is at http://www.yaketyyakallmouth.com/radiousefee/index.asp
i populate all the checkboxes from a database in asp and just want to send the total from one page to another
as i said i am useless at javascript!!!!!
@colinod
this will update the totals field when one of the checkboxes is clicked/changed.
in the end you will always have a form field with the correct total.
and the total is also saved in a global variable as well (accessible from everywhere) @colinod
submit the form? @colinod
no need to worry, I started there as well.
yes i thought of submitting the form but if you look at the page we want to seperate all the sections in the right column into their own pages and have tabs for them at the top of the page and i did not think you could submit the form to various pages
you can have tabs AND all the checkboxes in the same page. if you’re only submitting the total, you can use a separate form element just for the submit button.
EDIT: I can’t think of a way without using Javascript…
so i could give the page for the action on eack button or tab using javascript and abviously retrieve the total from the textbox in the form on the next page?
bad typing, i just meant i could set the page for the form action using javascript and get the totals from the form in the next page
just need to figure out how to set the action page for the form now
out of interest I did a benchmark on the two approaches.
result (tried with 1,000 and 10,000 elements):
adding up all values took between 100 and 111 milliseconds
adding a single value took between 0 and 10 milliseconds
the code I used - <script type="text/javascript">
-
// two div elements
-
var foo = document.getElementById("foo");
-
var bar = document.getElementById("bar");
-
-
// create 10,000 elements to query later
-
var p = document.createElement("span");
-
p.innerHTML = "1";
-
for (var i=0; i<10000; i++) {
-
foo.appendChild(p.cloneNode(true));
-
}
-
p_list = foo.getElementsByTagName("span");
-
-
// test 1: add a single value
-
function test1()
-
{
-
var anf = (new Date).getMilliseconds();
-
run : {
-
total = parseInt(document.getElementById("ich").value);
-
total += parseInt(this.firstChild.data);
-
}
-
var end = (new Date).getMilliseconds();
-
alert("Sum: " + total + " in " + (end-anf) + " ms");
-
}
-
// test 2: sum up over all elements
-
function test2()
-
{
-
var total = 0;
-
var anf = (new Date).getMilliseconds();
-
for (var i=0; i<10000; i++) {
-
total += parseInt(p_list[i].firstChild.data);
-
}
-
var end = (new Date).getMilliseconds();
-
alert("Sum: " + total + " in " + (end-anf) + " ms");
-
}
-
// start tests on click
-
p_list[1].addEventListener("click", test1, false);
-
bar.addEventListener("click", test2, false);
-
</script>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: George Stout |
last post by:
First off I do not know alot about writing queries to an Access
Database from an ASP page. This is why I need help.
I have an Events database for 6 colleges in our metro area. On the
homepage I...
|
by: Kelly Vernon |
last post by:
I have a standard ASP page that appends to an xml page.
Currently if there is more than one person attempting to append to the same
XML file at a time. One user will have the ability to append,...
|
by: curious_one |
last post by:
All,
I have a struct
struct {
char a;
char b;
}some_struct;
I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is...
|
by: estafford |
last post by:
I am having trouble writing a conditional block using ASP.NET and C#.
I am trying to do something like this:
1. if page is PostBack
- transfer to another page
2. if not postback
- connect...
|
by: Michelle A. |
last post by:
I have a four page form. Pages 1-3 stores there information in a session
variables. Page four is reached (a final review page) and then the
information will be written to the SQL server.
When...
|
by: Csaba Gabor |
last post by:
Firefox's configuration settings (Prefs.js) can be accomplished via the
interface at about:config.
Q1. Is there any such setting that can be repeatedly altered via
javascript (in a vanilla...
|
by: jonnyblazed |
last post by:
Okay..... I'm gonna sum up what I want to do cause it will make it
pretty simple for you guys to understand.
Here we go:
I have a form that posts data to a PHP mailer page I have created. When...
|
by: miladhatam |
last post by:
hi
i am milad and i have started learning asp.net c# for 2 months
how can i send parameter to a page with code
i did this work with html by writing the destination address in action
thanks
|
by: Julian32 |
last post by:
Hello,
I'm trying to keep track of purchases made from my website by having Paypal redirect users to a page on my site after purchase, and writing the information received to an Access Database....
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |