•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,473 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 5488 | Replies: 21
![]() |
| |
•
•
Join Date: Oct 2007
Posts: 38
Reputation:
Rep Power: 2
Solved Threads: 1
Hello,
I hope someone has the time to help me.
I have a form that lists a variable price in
<input type=hidden name=price value=1>
I have a drop down box letting the user choose a quantity in
<select name=quantity>
<option value=1>1</option>
<option value=2>2</option>
etc...
I have a html form input field that I want to show the price in
<input type=text name=total_price>
How do I hook all this up so that the "total_price" field will display the variable price and multiply it by the quantity option to give a total numerical figure of cost?
Thanks,
KD
I hope someone has the time to help me.
I have a form that lists a variable price in
<input type=hidden name=price value=1>
I have a drop down box letting the user choose a quantity in
<select name=quantity>
<option value=1>1</option>
<option value=2>2</option>
etc...
I have a html form input field that I want to show the price in
<input type=text name=total_price>
How do I hook all this up so that the "total_price" field will display the variable price and multiply it by the quantity option to give a total numerical figure of cost?
Thanks,
KD
If the price is a price that changes daily (such as gasoline) and is stored on your server, JavaScript can't do the job. You need a server-side script.
If the price varies with the quantity ordered, then you need a script that reads the quantity, looks the price up in an array (indexed by the quantity ordered) and then display it in a text box.
If the price varies with the quantity ordered, then you need a script that reads the quantity, looks the price up in an array (indexed by the quantity ordered) and then display it in a text box.
Last edited by MidiMagic : Oct 19th, 2007 at 1:28 am.
Daylight-saving time uses more gasoline
You need to put quote marks around the attributes "text" and "total_price".
You need to read the two values for price and quantity. Then you need to find the product. Finally, you must put the answer into the form:
You need to read the two values for price and quantity. Then you need to find the product. Finally, you must put the answer into the form:
var price, quantity; price = document.forms.nameOfForm.total_price.value; quantity = document.forms.nameOfForm.quan_ordered.value; revenue = price * quantity; document.forms.nameOfForm.part_revenue.value = revenue;
Daylight-saving time uses more gasoline
>price = document.forms.nameOfForm.total_price.value;
Incorrect way of accessing form elements. Each form object has an 'elements' host object which has all the form elements as it's properties. Correct way would be:
Incorrect way of accessing form elements. Each form object has an 'elements' host object which has all the form elements as it's properties. Correct way would be:
price = document.forms['nameOfForm'].elements['total_price'].value; I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
That syntax is necessary only if you are accessing the form using element numbers, variables containing the form or element names, or getElementByName.
None of my books on JavaScript show it for an element address not using variables. That is only one of many valid ways to address an element.
And it definitely does NOT work for addressing radio buttons, where you have to have an address of the syntax:
document.forms.myform.rabutton[i].checked
Is the W3C now deprecating JavaScript usage too?
None of my books on JavaScript show it for an element address not using variables. That is only one of many valid ways to address an element.
And it definitely does NOT work for addressing radio buttons, where you have to have an address of the syntax:
document.forms.myform.rabutton[i].checked
Is the W3C now deprecating JavaScript usage too?
Daylight-saving time uses more gasoline
> That syntax is necessary only if you are accessing the form using element numbers
Take a closer look at what I had posted. The fields or elements of a form should be accessed using the 'elements' nodelist or host object called HTMLCollection. Just because what you posted works now doesn't mean it always will.
> None of my books on JavaScript show it for an element address not using variables.
There are some things which you won't find in books. And anyways of the majority of books out there, only handful of them are worth reading.
> And it definitely does NOT work for addressing radio buttons
It certainly does. Please get your facts correct before putting a *NOT* there.
>Is the W3C now deprecating JavaScript usage too?
No, but you sure are using it the wrong way.
Take a closer look at what I had posted. The fields or elements of a form should be accessed using the 'elements' nodelist or host object called HTMLCollection. Just because what you posted works now doesn't mean it always will.
> None of my books on JavaScript show it for an element address not using variables.
There are some things which you won't find in books. And anyways of the majority of books out there, only handful of them are worth reading.
> And it definitely does NOT work for addressing radio buttons
It certainly does. Please get your facts correct before putting a *NOT* there.
>Is the W3C now deprecating JavaScript usage too?
No, but you sure are using it the wrong way.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
How are we supposed to know this "correct" way of doing things if most of the sources have not published it? Clairvoyance?
What is your source for this requirement of using the 'elements' nodelist? All of my sources say that it is becoming the obsolete way of addressing elements.
Are we talking about the necessity of including the "elements" name in the description?
Or are we talking about different syntax? These are equivalent, unless you want the program to be able to change parts of the descriptor (which requires the third case):
document.forms.nameOfForm.part_revenue.value = revenue;
document.forms['nameOfForm'].elements['part_revenue'].value = revenue;
a = 'nameOfForm'; b = 'part_revenue'; document.forms[a].elements[b].value = revenue;
What is the "correct" syntax for using a radio button? Is it addressed as an array of arrays? I said it doesn't work, because I can't get it to work. If I try to address the element as a subscript, than I can't seem to address its own subscript.
What is your source for this requirement of using the 'elements' nodelist? All of my sources say that it is becoming the obsolete way of addressing elements.
Are we talking about the necessity of including the "elements" name in the description?
Or are we talking about different syntax? These are equivalent, unless you want the program to be able to change parts of the descriptor (which requires the third case):
document.forms.nameOfForm.part_revenue.value = revenue;
document.forms['nameOfForm'].elements['part_revenue'].value = revenue;
a = 'nameOfForm'; b = 'part_revenue'; document.forms[a].elements[b].value = revenue;
What is the "correct" syntax for using a radio button? Is it addressed as an array of arrays? I said it doesn't work, because I can't get it to work. If I try to address the element as a subscript, than I can't seem to address its own subscript.
Last edited by MidiMagic : Oct 28th, 2007 at 3:07 pm.
Daylight-saving time uses more gasoline
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation:
Rep Power: 5
Solved Threads: 48
form != input..
name is XHTML deprecated as an attribute of the form element, that is <form...>. It's not deprecated on any of the form fields. The validator can't be wrong, it's fed the DTD/schema and it validates the input page; the schema/DTD can't be wrong; because they ARE XHTML 1.0. If the validator program itself was wrong... meh.. it's not.
I guess what people are trying to say is that name is not deprecated on the 'tags that make up forms'. which is a little ambiguous I'll admit.. but, read the posts, look at the context of examples.. name continues to be a valid attribute of input, select, submit and other form fields, and it is indeed needed for those fields to be submitted when the form is.
name is XHTML deprecated as an attribute of the form element, that is <form...>. It's not deprecated on any of the form fields. The validator can't be wrong, it's fed the DTD/schema and it validates the input page; the schema/DTD can't be wrong; because they ARE XHTML 1.0. If the validator program itself was wrong... meh.. it's not.
I guess what people are trying to say is that name is not deprecated on the 'tags that make up forms'. which is a little ambiguous I'll admit.. but, read the posts, look at the context of examples.. name continues to be a valid attribute of input, select, submit and other form fields, and it is indeed needed for those fields to be submitted when the form is.
Plato forgot the nullahedron..
> How are we supposed to know this "correct" way of doing things if most of the sources
> have not published it?
It's a real pity there are a lot of substandard tutorials / books out there to misguide beginners. Good sources are hard to find if one doesn't know what he is looking for.
> What is your source for this requirement of using the 'elements' nodelist?
DOM specification drafted by W3C.
> All of my sources say that it is becoming the obsolete way of addressing elements.
You are in bad company.
> What is the "correct" syntax for using a radio button?
Read this thoroughly for form access related queries. Then this.
> have not published it?
It's a real pity there are a lot of substandard tutorials / books out there to misguide beginners. Good sources are hard to find if one doesn't know what he is looking for.
> What is your source for this requirement of using the 'elements' nodelist?
DOM specification drafted by W3C.
> All of my sources say that it is becoming the obsolete way of addressing elements.
You are in bad company.
> What is the "correct" syntax for using a radio button?
Read this thoroughly for form access related queries. Then this.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- need help finding a script or tool to use for a news update program. (PHP)
- sql query problem with MS Access and C# (C#)
- Which of these current affairs most worries you? (IT Professionals' Lounge)
- Need help debugging program (Java)
- Populating a fields on a form base on a selected item from a droplist. (PHP)
- HELP with VB project (Visual Basic 4 / 5 / 6)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Trouble with changng the scroll color
- Next Thread: ClamShell Menu Not Working in Foxfire



Hybrid Mode