reading a value from a dropdown box on a form

N

Nick Calladine

hi

I am trying to work out if i can read a value from a dropdown box to
caculate a running total on selection of a form

I using romancart as way to get the data to the form but wish to have a
simple total on screen so when user change the option / upgrade it displays
the value on screen before posting the form

i have done very little javascript so pls if you could explain or least show
some rough code example that would be appreciated

here my code so far

<SCRIPT LANGUAGE="JavaScript">
function alerter() {
alert(itemname3)
}


<form action=http://www.romancart.com/cart.asp name="myform">
<input type=hidden name=displaycart value=no>
<input type=hidden name=storeid value=10000>

<select name=itemname3 size="1"
onchange="alerter(document.myform.itemname3)">
<option value="2800 {10.00}">2800 AMD 64</option>
<option value="3000 {20.00}">3000 AMD 64</option>
<option value="3200 {30.00}">3200 AMD 64</option>
<option value="3400 {40.00}">3400 AMD 64</option>
<option value="3700 {50.00}">3700 AMD 64</option>
</select>

i understand i would need to parse the value to get the number inside the
curly brackets but i really not too sure of what i am doing.

can someone give me some ideas of where i going wrong ...

thanks
 
M

Mick White

Nick said:
hi

I am trying to work out if i can read a value from a dropdown box to
caculate a running total on selection of a form

I using romancart as way to get the data to the form but wish to have a
simple total on screen so when user change the option / upgrade it displays
the value on screen before posting the form

i have done very little javascript so pls if you could explain or least show
some rough code example that would be appreciated

here my code so far

<SCRIPT LANGUAGE="JavaScript">
function alerter() {
alert(itemname3)
}


<form action=http://www.romancart.com/cart.asp name="myform">
<input type=hidden name=displaycart value=no>
<input type=hidden name=storeid value=10000>

<select name=itemname3 size="1"
onchange="alerter(document.myform.itemname3)">
<option value="2800 {10.00}">2800 AMD 64</option>
<option value="3000 {20.00}">3000 AMD 64</option>
<option value="3200 {30.00}">3200 AMD 64</option>
<option value="3400 {40.00}">3400 AMD 64</option>
<option value="3700 {50.00}">3700 AMD 64</option>
</select>

i understand i would need to parse the value to get the number inside the
curly brackets but i really not too sure of what i am doing.

can someone give me some ideas of where i going wrong ...
onchange=
"alert(parseFloat(this[this.selectedIndex].value.split('{')[1],10))">

Mick
 
N

Nick Calladine

Thanks mick for the fast response....
easy when u know how..

is there a way that i can get the value within the brackets
ie. 10.00 and 20.00 and store it as a numeric / currency easily or will i
have to go and loop through the return value to manual get what's is between
the { } and then convert it to a numeric value

thanks once again

Nick

Mick White said:
Nick said:
hi

I am trying to work out if i can read a value from a dropdown box to
caculate a running total on selection of a form

I using romancart as way to get the data to the form but wish to have a
simple total on screen so when user change the option / upgrade it
displays the value on screen before posting the form

i have done very little javascript so pls if you could explain or least
show some rough code example that would be appreciated

here my code so far

<SCRIPT LANGUAGE="JavaScript">
function alerter() {
alert(itemname3)
}


<form action=http://www.romancart.com/cart.asp name="myform">
<input type=hidden name=displaycart value=no>
<input type=hidden name=storeid value=10000>

<select name=itemname3 size="1"
onchange="alerter(document.myform.itemname3)">
<option value="2800 {10.00}">2800 AMD 64</option>
<option value="3000 {20.00}">3000 AMD 64</option>
<option value="3200 {30.00}">3200 AMD 64</option>
<option value="3400 {40.00}">3400 AMD 64</option>
<option value="3700 {50.00}">3700 AMD 64</option>
</select>

i understand i would need to parse the value to get the number inside the
curly brackets but i really not too sure of what i am doing.

can someone give me some ideas of where i going wrong ...
onchange=
"alert(parseFloat(this[this.selectedIndex].value.split('{')[1],10))">

Mick
 
R

RobG

Nick said:
Thanks mick for the fast response....
easy when u know how..

is there a way that i can get the value within the brackets
ie. 10.00 and 20.00 and store it as a numeric / currency easily or will i
have to go and loop through the return value to manual get what's is between
the { } and then convert it to a numeric value

Please don't top-post. Reply just below a trimmed quote of what you
are replying to.

You can use a regular expression to get the stuff inside the
braces:

function getCash(x){
var y = x.replace(/^.*\{|\}/g,'');
alert(y);
}

getCash( '2800 {10.00}' );


'y' will be string, to convert it to a number, either use
parseFloat() or the unary '+' operator:

var y = parseFloat( x.replace(/^.*\{|\}/g,'') );

or

var y = +x.replace(/^.*\{|\}/g,'');

The '+' must appear immediately before 'x' with no whitespace.
Converting y to a number will cause the decimal places to disappear
(if y is an integer like 10.00), but you can use toFixed() to display
2 decimal places in output:

alert( y ); // --> 10
alert( y.toFixed(2) ); // --> 10.00

Usually formatting is only applied for output intended for human
consumption (unless we're talking COBOL or fixed-length records...).
 
N

Nick Calladine

getcash(2300 {10.05});
function getCash(x){
var y = x.replace(/^.*\{|\}/g,'');
alert(y);

Hi Rob

can you please explain how it get the information from the bracket from the
replace function as i cant see how it works.. ?
it just doesnt make any sense...

Nick
 
M

Mick White

Nick said:
Thanks mick for the fast response....
easy when u know how..

is there a way that i can get the value within the brackets
ie. 10.00 and 20.00 and store it as a numeric / currency easily or will i
have to go and loop through the return value to manual get what's is between
the { } and then convert it to a numeric value

onchange= alert("$"+
this[this.selectedIndex].value.split('{')[1].replace(/[^\d.]/g,"")))

Mick.
 
N

Nick Calladine

onchange= alert("$"+
this[this.selectedIndex].value.split('{')[1].replace(/[^\d.]/g,"")))

Mick.

can you please explain how it get the information from the bracket from the
replace function as i cant see how it works.. ?
it just doesnt make any sense...

Nick
 
M

Mick White

Nick said:
onchange= alert("$"+
this[this.selectedIndex].value.split('{')[1].replace(/[^\d.]/g,"")))

Mick.


can you please explain how it get the information from the bracket from the
replace function as i cant see how it works.. ?
it just doesnt make any sense...
<select name=itemname3 size="1"
onchange="alert("$"+
this[this.selectedIndex].value.split('{')[1].replace(/[^\d.]/g,"")))"<option value="2800 {10.00}">2800 AMD 64</option>
<option value="3000 {20.00}">3000 AMD 64</option>
<option value="3200 {30.00}">3200 AMD 64</option>
<option value="3400 {40.00}">3400 AMD 64</option>
<option value="3700 {50.00}">3700 AMD 64</option>
</select>

Let's say you select the second option

this[this.selectedIndex].value == "3000 {20.00}"

"3000 {20.00}".split('{')[1] == "20.00}"
//create an array from string, the divider being "{", the second entry
("[1]") of the array="20.00}"

"20.00}".replace(/[^\d.]/g,"") == "20.00"
// remove anything that is not a digit or a dot.

alert("$"+"20.00") == $20.00

Mick
 
N

Nick Calladine

Mick

thanks for your help it is most appreciated

thanks
a very gratefull and slow learner

Nick
 
D

Dr John Stockton

JRS: In article <42aed23f$0$22907$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Tue, 14 Jun 2005 22:49:12, seen in
news:comp.lang.javascript said:
or the unary '+' operator:
The '+' must appear immediately before 'x' with no whitespace.

Which browsers require absence of whitespace?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top