reading a value from a dropdown box on a form

Discussion in 'Javascript' started by Nick Calladine, Jun 14, 2005.

  1. 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
    Nick Calladine, Jun 14, 2005
    #1
    1. Advertising

  2. Nick Calladine

    Mick White Guest

    Nick Calladine wrote:

    > 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
    Mick White, Jun 14, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:WHzre.11757$...
    > Nick Calladine wrote:
    >
    >> 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
    Nick Calladine, Jun 14, 2005
    #3
  4. Nick Calladine

    RobG Guest

    Nick Calladine wrote:
    > 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...).



    --
    Rob
    RobG, Jun 14, 2005
    #4
  5. 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
    Nick Calladine, Jun 14, 2005
    #5
  6. Nick Calladine

    Mick White Guest

    Nick Calladine wrote:

    > 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.
    Mick White, Jun 14, 2005
    #6
  7. > 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
    Nick Calladine, Jun 14, 2005
    #7
  8. Nick Calladine

    Mick White Guest

    Nick Calladine wrote:

    >>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
    Mick White, Jun 14, 2005
    #8
  9. Mick

    thanks for your help it is most appreciated

    thanks
    a very gratefull and slow learner

    Nick
    Nick Calladine, Jun 14, 2005
    #9
  10. 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, RobG <> posted :

    >or the unary '+' operator:


    > The '+' must appear immediately before 'x' with no whitespace.


    Which browsers require absence of whitespace?

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jun 14, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy

    Dropdown List Box - Adding an item value

    Brian Candy, Feb 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    443
    Jose Marcenaro
    Feb 20, 2004
  2. Scott Allen
    Replies:
    1
    Views:
    855
    Scott Allen
    Aug 25, 2004
  3. vishnu
    Replies:
    1
    Views:
    1,187
    Patrick.O.Ige
    Mar 25, 2006
  4. Mike
    Replies:
    1
    Views:
    793
  5. Gnarlodious
    Replies:
    4
    Views:
    17,553
    elizas
    May 5, 2010
Loading...

Share This Page