I have a very simple price times quantity calculation.
The input is STWCORQuant: - <input name="STWCORQuant" class="contactitalicbold" id="STWCORQuant" onChange="javascript:STWCORPrice();" value="0" size="8" style="text-align: right; font-family: ver">
And the fucntion is STWCORPrice(): -
<script language="javascript" type="text/javascript">
-
-
function STWCORPrice() {
-
var price = 0;
-
var shipping = 0;
-
var total=0;
-
var qnty=document.all.STWCORQuant.value;
-
-
if (qnty > 49 && qnty < 75){
-
price = 23;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 74 && qnty < 100){
-
price = 17;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 99 && qnty < 200){
-
price = 14;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 199 && qnty < 250){
-
price = 13.75;
-
shipping = 7.75;
-
}
-
-
total = (price * qnty)+shipping;
-
document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
-
}
-
-
</script>
-
The output is this: - <td width="15%" id="STWCORIssueCost" Font Class="sidemenutextgrn" align="right">$0</Font></td>
It doesn't work, but looks like it should. When a quantity is input and the box is tabbed out of, nothing happens. I can't tell what is wrong....need help!
Thanks
12 2529 Dormilich 8,658
Recognized Expert Moderator Expert
you should use document.getEle mentById() instead of document.all (that's IE syntax and not understood by many browsers).
rename onChange to onchange.
what is the value of qnty?
you could also call your function as STWCORPrice(thi s.value) (adjust function code...), so you don't need to fetch the value from the document tree.
regards
you should use document.getEle mentById() instead of document.all (that's IE syntax and not understood by many browsers).
rename onChange to onchange.
what is the value of qnty?
you could also call your function as STWCORPrice(thi s.value) (adjust function code...), so you don't need to fetch the value from the document tree.
regards
I changed line five of the code per your suggestion: -
-
function STWCORPrice() {
-
var price = 0;
-
var shipping = 0;
-
var total=0;
-
var qnty=GetElementById(STWCORQuant);
-
-
if (qnty > 49 && qnty < 75){
-
price = 23;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 74 && qnty < 100){
-
price = 17;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 99 && qnty < 200){
-
price = 14;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 199 && qnty < 250){
-
price = 13.75;
-
shipping = 7.75;
-
}
-
-
total = (price * qnty)+shipping;
-
document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
-
}
-
-
The value of qnty is the quantity input by the user (variable id is "STWCORQuan t")
It still doesn't work...and the onchange vs. OnChange made no difference when I tried it (all the other OnChange events are capitalized the same way).
Have I used the GetElementById correctly?
Dormilich 8,658
Recognized Expert Moderator Expert
you don't have. in your case you have to call it as - var qnty = document.getElementById("STWCORQuant");
to get the value of qnty add somewhere after its definition
this will show an alert box with qnty's value.
regards
OK...we have made some progress...I fixed the code per your suggestion. It is now: -
function STWCORPrice() {
-
var price = 0;
-
var shipping = 0;
-
var total=0;
-
var qnty=document.getElementById("STWCORQuant");
-
alert(qnty);
-
-
if (qnty > 49 && qnty < 75){
-
price = 23;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 74 && qnty < 100){
-
price = 17;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 99 && qnty < 200){
-
price = 14;
-
shipping = 6.25;
-
}
-
-
else if (qnty > 199 && qnty < 250){
-
price = 13.75;
-
shipping = 7.75;
-
}
-
-
total = (price * qnty)+shipping;
-
document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
-
}
-
-
However, when the OnChange is triggered with an input, the result is an alert box that simply says: [Object]
So, the Onchange is working to call the function, but the input variable STWCORQuant is not making it to the alert correctly in these two lines: -
var qnty=document.getElementById("STWCORQuant");
-
alert(qnty);
-
We are closer, but there seems to be a problem calling the variable and using it in the definition of qnty.
The input line is: -
<input name="STWCORQuant" class="contactitalicbold" id="STWCORQuant" onChange="javascript:STWCORPrice();" value="0" size="8" style="text-align: right; font-family: ver">
-
Any suggestions?
gits 5,390
Recognized Expert Moderator Expert
when you use: - var qnty = document.getElementById("STWCORQuant");
then qnty stores a reference to the node ... you just need to retrieve its value with: - var qnty = document.getElementById("STWCORQuant").value;
or alert it as:
kind regards
Great! Now the alert correctly shows the value, but the output on the page remains $0 (nothing changes). The output code is: -
document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
-
I know the code is now working up to this point because I "alerted" all the variables in the lines just before this and they all are correct.
I also tried: -
document.all("STWCORIssueCost").value= '$ '+ total.toFixed(2);
-
The place where it goes on the page is: -
<td width="15%" id="STWCORIssueCost" Font Class="sidemenutextgrn" align="right">$0</Font></td>
-
Can you see why the total is not being displayed at STWCORIssueCost ?
acoder 16,027
Recognized Expert Moderator MVP
Two issues:
1. Use document.getEle mentById() in place of document.all.
2. Use innerHTML in place of .value because a td element doesn't have a value.
It works...thank you very much!
acoder 16,027
Recognized Expert Moderator MVP
Glad it does! Post again if you have more questions and we'll be happy to help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: bin_P19 P |
last post by:
the code i have got is as follows and now im stuck
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Shopping Cart</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<link rel="StyleSheet" href="css/style.css" type="text/css">
|
by: ChadDiesel |
last post by:
Hello everyone. I need some advice on table structure for a new project
I've been given.
One of our customers sends us an Excel spreadsheet each week containing
their order. Currently, someone formats the spreadsheet, prints it out, and
manually picks out the products we need to ship. I want to import this into
an Access table. ...
|
by: Shannan Casteel via AccessMonster.com |
last post by:
I have a subform for listing parts. It has fields including: ClaimID, ITEM,
NET PRICE, LIST PRICE, Quantity, Supplier, and a calculated field called Part
Total. The subform is based on a query. The Part Total field is calculated
as (=.*.). Then there is a
hidden text box on my subform that calculates the sum of all the Part Totals
for...
|
by: eyoung |
last post by:
I call a function that takes the unit price and quantity ordered to
create an amount...it looks something like this.
function calculateCost()
{
quantity=document.RFO.quantity.value;
unitPrice=document.RFO.unitPrice.value;
total=0;
if(isPositiveInteger(quantity))
{
|
by: Srikanth |
last post by:
Hi,
My requirement is to have the datagrid perform the work of an order
entry system. The specifications are something like this. I have got a
table in MS-Access, named master_software, whose fields are: soft_name,
edition, manufacturer, price.
My data grid would contain the following columns: Software Name,
| |
by: lucky29105 |
last post by:
Hello- I cant seem to figure out why I am getting the following error:
cannot find symbol : class CD
location: class Inventory5
CD cd = (CD)supplies;
^
I'm getting this on several lines, and something tells me it is a silly error, but I can't find it. Here is the part of the code where I get the errors:
CD cd = (CD)supplies;
|
by: wynn |
last post by:
I had created a shopping cart website using visual studio 2005 web developer
I got this error when i doing update the shopping cart, can anyone please help me solve this problem thanks very much
Dim cn As New SqlClient.SqlConnection()
cn.ConnectionString =...
|
by: jcato77 |
last post by:
I am having trouble figuring out my code and was hoping someone could point me in the right direction. Below is my code what I need to due is create a method to add and display the value of the entire inventory. I have what I think is the correct code but it's not working. Help is greatly appreciated
class Inventory3
{
public static...
|
by: Luron31 |
last post by:
Hello everyone,
im doing a project and im really stuck on it...
i dont want answers. i have a very guilty conscience. i just need some hints as to what im doing wrong or what i could do right...
"A VendingMachine object represents a vending machine that sells products like candy or soda. It has a fixed number of slots, each of which can...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |