Drop down menu (syntax problem)

M

MickG

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
 
M

Mick White

MickG said:
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
 
R

RobB

MickG said:
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.
 
M

MickG

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
 
R

RobG

MickG said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top