Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Drop down menu (syntax problem)

Reply
Thread Tools

Drop down menu (syntax problem)

 
 
MickG
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
 
 
 
RobB
Guest
Posts: n/a
 
      03-15-2005
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.

 
Reply With Quote
 
MickG
Guest
Posts: n/a
 
      03-15-2005
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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-18-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
retrive preselected value in second drop down list from the first drop down list weiwei ASP .Net 0 01-05-2007 07:29 PM
Multiple select drop down poplulating sub drop down scott@dettcom.com Javascript 3 11-09-2006 07:50 PM
Auto Drop down a Drop Down List xxbmichae1@supergambler.com Javascript 5 11-23-2005 01:35 AM
New to .NET, can I have one drop down box control the data of another drop down box using a database? SirPoonga ASP .Net 2 01-07-2005 10:44 PM



Advertisments