473,560 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select 1 value from multiple...

I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","T hree","Four","F ive","Six"} [J] ;
.... so K would hold "Five" in this case.

Thx
Aug 10 '05 #1
4 1381

"Noozer" <do*******@me.h ere> wrote in message
news:zcjKe.1728 65$s54.111428@p d7tw2no...
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","T hree","Four","F ive","Six"} [J] ;
... so K would hold "Five" in this case.

Thx
Use an array.
var K=new Array("One","Tw o","Three","Fou r","Five","Six" );
var J=4;
document.write( K[J]);
would produce 'five' remember that arrays start with a zero offset

Best wishes

Darren

Aug 10 '05 #2
Noozer wrote:
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","T hree","Four","F ive","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= (new Array("One","Tw o","Three","Fou r","Five","Six" ))[J-1];

--
Klaus Johannes Rusch
Kl********@atme dia.net
http://www.atmedia.net/KlausRusch/
Aug 10 '05 #3

"Klaus Johannes Rusch" <Kl********@atm edia.net> wrote in message
news:3l******** *****@individua l.net...
Noozer wrote:
I want to return one of several values, based on the value of a variable.
Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","T hree","Four","F ive","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= (new Array("One","Tw o","Three","Fou r","Five","Six" ))[J-1];


Just wondering if there was a way to combine it on one line... No problem.
This works.
Aug 10 '05 #4
JRS: In article <zcjKe.172865$s 54.111428@pd7tw 2no>, dated Wed, 10 Aug
2005 08:49:35, seen in news:comp.lang. javascript, Noozer
<do*******@me.h ere> posted :
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","T hree","Four","F ive","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= [,"One","Two","T hree","Four","F ive","Six"][J] ;

Changed {} to [] and insert comma.

Actually, K holds a reference to "Five"; in this case, that does not
matter, but in others it might.

Note that (AIUI) the array should be created whenever used; the
following would be more efficient in that case.

var Numbers = [,"One","Two","T hree","Four","F ive","Six"] ; // Global
....
var J = 5 ;
var K = Numbers[J] ;

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7708
by: Rolf Wester | last post by:
Hi, I have a form with a select element with multiple="true". When using the GET method (I suppose the same happens with the POST method) I can seen that the form sends channels=CH1&channels=CH2 when CH1 and CH2 have been choosen. $_GET gives me "CH2". Is there any way to get all the choosen channels elements? I would be very appreciative...
7
7886
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option value='43'>Aisha</option> <option value='40'>Amsterdam</option> <option value='55'>Asmaa</option> <option value='20'>Beau Rivage</option> <option value='48'>Blue...
6
4278
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. Based on the number of variables passed through a GET string, I want to multiply the total number of selected items for each together to see how many...
5
2379
by: callmebill | last post by:
I'm relatively new to javascript, and I'm trying to decide whether the following (and if so, clues on how to do it): I'd like to create two HTML multiple-select boxes. The first would be a list of items, and the second would be a list of categories. By clicking a category in the 2nd box, the members of that category (from the first box)...
2
3638
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code /////////////////////////////////////////////////////////////////////////////////////// <form action="whatever.php" method="post"> <select name="zip_code"...
2
3100
by: murraymiken | last post by:
I'm looking to have multiple multiple-select-boxes on a page. But I can only get the contents from the last selected value within a box, via PHP. I've tried numerous methods. What am I doing wrong? You can see ALL the values present in the url:...
1
5869
by: abhishekhs | last post by:
Hi all I have more than one multiple select lists in a page. Something like this <tr> <td> <select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px"> <? for ($i=0; $i < $rows; $i++)
3
8707
by: imrantbd | last post by:
I need array type name like "destList" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList" select box and send all selected value in php page.The multiple select value then insert in database added by comma.The following is my code: Form Page:form.php <head> <script...
25
5375
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. Basically i have a multiple select box. An it works except for this one part i want to add to it.The first box i have is called project members which...
6
7545
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created the problem that when I went to select the other drop down menus, the selections i made on the multiple select one would clear. Then I had tried putting...
0
7635
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7556
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5176
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
882
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.