Drop down menu (syntax problem)

Discussion in 'Javascript' started by MickG, Mar 14, 2005.

  1. MickG

    MickG Guest

    Hi,

    I'm wanting to add some code to my drop down list that changes the
    value of a variable depending on the choice made, ie if none is the
    option selected, then the variable cost is set to zero, if standard,
    then cost =3 and so on.

    <select name="bookwrapping">
    <option value="none" selected disabled>none</option>
    <option value="Standard">Standard Wrapping</option>
    <option value="Deluxe">Deluxe Wrapping</option></p>

    Thanks in advance.

    Michael
     
    MickG, Mar 14, 2005
    #1
    1. Advertising

  2. MickG

    Mick White Guest

    MickG wrote:

    > Hi,
    >
    > I'm wanting to add some code to my drop down list that changes the
    > value of a variable depending on the choice made, ie if none is the
    > option selected, then the variable cost is set to zero, if standard,
    > then cost =3 and so on.

    <select name="bookwrapping" onchange="if(this.selectedIndex)cost=
    +(this[this.selectedIndex].value);">
    <option value="0" selected disabled>none</option>
    <option value="1">Standard Wrapping</option>
    <option value="2">Deluxe Wrapping</option>

    Mick
     
    Mick White, Mar 14, 2005
    #2
    1. Advertising

  3. MickG

    RobB Guest

    MickG wrote:
    > Hi,
    >
    > I'm wanting to add some code to my drop down list that changes the
    > value of a variable depending on the choice made, ie if none is the
    > option selected, then the variable cost is set to zero, if standard,
    > then cost =3 and so on.
    >
    > <select name="bookwrapping">
    > <option value="none" selected disabled>none</option>
    > <option value="Standard">Standard Wrapping</option>
    > <option value="Deluxe">Deluxe Wrapping</option></p>
    >
    > Thanks in advance.
    >
    > Michael


    Variables - more than one - *are* changed when a user manipulates a
    listbox. It generally makes more sense to simply read the current value
    out as needed (at run-time) with whatever (presumably a totaling)
    function you're calling.
     
    RobB, Mar 15, 2005
    #3
  4. MickG

    MickG Guest

    My new code is
    <select name="bookwrapping"
    onchange="if(this.selectedIndex)wrap=+(this[this.selectedIndex].value);">
    <option value ="0">none</option>
    <option value="2.95">Standard Wrapping</option>
    <option value="3.95">Deluxe Wrapping</option>

    My problem is now that I cannot get the value to return as 0, 2.95 and
    3.95 work fine, but it states that 'wrap' has not been allocated a
    value.

    Any more help would be greatly appreciated.

    Thanks
     
    MickG, Mar 15, 2005
    #4
  5. MickG

    RobG Guest

    MickG wrote:
    > My new code is
    > <select name="bookwrapping"
    > onchange="if(this.selectedIndex)wrap=+(this[this.selectedIndex].value);">
    > <option value ="0">none</option>
    > <option value="2.95">Standard Wrapping</option>
    > <option value="3.95">Deluxe Wrapping</option>
    >
    > My problem is now that I cannot get the value to return as 0, 2.95 and
    > 3.95 work fine, but it states that 'wrap' has not been allocated a
    > value.
    >
    > Any more help would be greatly appreciated.
    >
    > Thanks


    'wrap' is only modified when on-change fires. Seems 'wrap'
    is a global variable, do you initialise it anywhere? If not, and
    the onchange doesn't fire, then 'wrap' is never created.

    It's always a bit dangerous to just let variables be created
    randomly throughout a page. All globals are better created in a
    single spot (for maintenance/bug fixing reasons) so initialise
    'wrap' and set it to zero just inside the very first script
    element in your page:

    ...
    <script type="text/javascript">
    var wrap = 0;
    ...

    </script>

    Now if the select's onchange is not called, wrap exists and is
    zero.


    --
    Rob
     
    RobG, Mar 18, 2005
    #5
    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. SirPoonga
    Replies:
    2
    Views:
    806
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,052
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    527
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    304
  5. Replies:
    3
    Views:
    315
Loading...

Share This Page