| | |
drop down quantity box to update price
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2007
Posts: 38
Reputation:
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
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.
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
> 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.
OK, the pages you gave me even show the syntax I used as an alternative. I think you are interpreting them in a stricter sense than is required.
But they are also using deprecated forms in their pages, so they are not current. The name attribute is now deprecated in the form tag. The only place it works and validates is in the radio button. (I must use XHTML.)
And I see the difference between what I tried to use for radio buttons and what works. I was trying:
It should be:
But the following ARE completely interchangeable, according to those pages. One is not better than the other, unless dynamic addressing is needed:
The reason to use the first version is to keep the files short. Some ISPs (including mine) charge more for longer downloads. Keeping the files short allows more downloads per hour with the cheaper plan.
But they are also using deprecated forms in their pages, so they are not current. The name attribute is now deprecated in the form tag. The only place it works and validates is in the radio button. (I must use XHTML.)
And I see the difference between what I tried to use for radio buttons and what works. I was trying:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
buton[i] = document.forms['namForm'].elements['buttonset[i]'].checked;
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
buton[i] = document.forms['namForm'].elements['buttonset'].[i].checked;
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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;
The reason to use the first version is to keep the files short. Some ISPs (including mine) charge more for longer downloads. Keeping the files short allows more downloads per hour with the cheaper plan.
Last edited by MidiMagic; Nov 2nd, 2007 at 2:44 am.
Daylight-saving time uses more gasoline
![]() |
Similar Threads
- sql query problem with MS Access and C# (C#)
- need help finding a script or tool to use for a news update program. (PHP)
- 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
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser bug captchaformproblem cart checkbox child class close codes createrange() css cursor date debugger decimal dependent design disablefirebug dom dropdown editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming progressbar prototype redirect runtime safari scale scriptlets scroll search security shopping size software toggle unicode w3c web wysiwyg \n






