By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,773 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,773 IT Pros & Developers. It's quick & easy.

Drop down menu (syntax problem)

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
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
Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.