Drop down menu (syntax problem)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MickG

    Drop down menu (syntax problem)

    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="bookwrapp ing">
    <option value="none" selected disabled>none</option>
    <option value="Standard ">Standard Wrapping</option>
    <option value="Deluxe"> Deluxe Wrapping</option></p>

    Thanks in advance.

    Michael
  • Mick White

    #2
    Re: Drop down menu (syntax problem)

    MickG wrote:
    [color=blue]
    > 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.[/color]
    <select name="bookwrapp ing" onchange="if(th is.selectedInde x)cost=
    +(this[this.selectedIn dex].value);">
    <option value="0" selected disabled>none</option>
    <option value="1">Stand ard Wrapping</option>
    <option value="2">Delux e Wrapping</option>

    Mick

    Comment

    • RobB

      #3
      Re: Drop down menu (syntax problem)

      MickG wrote:[color=blue]
      > 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="bookwrapp ing">
      > <option value="none" selected disabled>none</option>
      > <option value="Standard ">Standard Wrapping</option>
      > <option value="Deluxe"> Deluxe Wrapping</option></p>
      >
      > Thanks in advance.
      >
      > Michael[/color]

      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.

      Comment

      • MickG

        #4
        Re: Drop down menu (syntax problem)

        My new code is
        <select name="bookwrapp ing"
        onchange="if(th is.selectedInde x)wrap=+(this[this.selectedIn dex].value);">
        <option value ="0">none</option>
        <option value="2.95">St andard Wrapping</option>
        <option value="3.95">De luxe 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

        Comment

        • RobG

          #5
          Re: Drop down menu (syntax problem)

          MickG wrote:[color=blue]
          > My new code is
          > <select name="bookwrapp ing"
          > onchange="if(th is.selectedInde x)wrap=+(this[this.selectedIn dex].value);">
          > <option value ="0">none</option>
          > <option value="2.95">St andard Wrapping</option>
          > <option value="3.95">De luxe 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[/color]

          '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

          Comment

          Working...