473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Passing var into object name?

Dear All,

I have RTFM'd until I am blue in the face and I am completely at a loss.
I would be grateful for any help.

I have a series of elements in my html called var1, var2, var3 etc

I want to be able to pass a number to a function to get details of that
property such that eg...

myFunction(varno)
{
alert(document.form1.var[varno].foo);

}

so that if I go myFunction(2)

then it goes

alert(document.form1.var2.foo);

I have tried casting, the + operator, square brackets, quotes, you name
it but I can't find the correct syntax.

Would be very grateful for some help

Yours,
TIU
May 1 '06 #1
9 1454
Did you try this: document.all["var" + varno] I'm a bit rusty but I
recall using this idiom many times in the past and it worked for me.

Ken

May 1 '06 #2
turnitup said the following on 5/1/2006 3:06 PM:
Dear All,

I have RTFM'd until I am blue in the face and I am completely at a loss.
I would be grateful for any help.
Then you either didn't read the right part or you read the wrong manual :)
I have a series of elements in my html called var1, var2, var3 etc

I want to be able to pass a number to a function to get details of that
property such that eg...

myFunction(varno)
{
alert(document.form1.var[varno].foo);
document.form1.elements['var' + varno].foo;
}

so that if I go myFunction(2)

then it goes

alert(document.form1.var2.foo);
document.form1.elements['var' + varno].foo;
I have tried casting, the + operator, square brackets, quotes, you name
it but I can't find the correct syntax.

Would be very grateful for some help


If the above doesn't work, show some sample code.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #3
Kenosis said the following on 5/1/2006 3:19 PM:
Did you try this:
Did you try quoting what you are replying to?

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
<URL: http://www.safalra.com/special/googlegroupsreply/ >
document.all["var" + varno] I'm a bit rusty but I
recall using this idiom many times in the past and it worked for me.


It does, in browsers that support document.all, and there is an element
with a name or id that is var#

If you are rusty enough that your common idiom was document.all then
maybe it is time to brush up a little on JS :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #4
Randy Webb wrote:
turnitup said the following on 5/1/2006 3:06 PM:
Dear All,

I have RTFM'd until I am blue in the face and I am completely at a
loss. I would be grateful for any help.


Then you either didn't read the right part or you read the wrong manual :)
I have a series of elements in my html called var1, var2, var3 etc

I want to be able to pass a number to a function to get details of
that property such that eg...

myFunction(varno)
{
alert(document.form1.var[varno].foo);


document.form1.elements['var' + varno].foo;
}

so that if I go myFunction(2)

then it goes

alert(document.form1.var2.foo);


document.form1.elements['var' + varno].foo;
I have tried casting, the + operator, square brackets, quotes, you
name it but I can't find the correct syntax.

Would be very grateful for some help


If the above doesn't work, show some sample code.

Thanks very much, that did work. I am confused though as to why I cannot
simply use a syntactical mechanism to call "document.form1.var2.foo"
when I can call it directly!

As you say - wrong part of TFM!!
May 1 '06 #5
turnitup wrote:
I have a series of elements in my html called var1, var2, var3 etc
I want to be able to pass a number to a function to get details of
that property such that eg...


See the very first section of
http://www.javascripttoolbox.com/bestpractices/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #6
Matt Kruse wrote:
turnitup wrote:
I have a series of elements in my html called var1, var2, var3 etc
I want to be able to pass a number to a function to get details of
that property such that eg...


See the very first section of
http://www.javascripttoolbox.com/bestpractices/


Matt,

That looks like an excellent resource. Thank you.
May 1 '06 #7
turnitup said on 02/05/2006 5:41 AM AEST:
[...]

Thanks very much, that did work. I am confused though as to why I cannot
simply use a syntactical mechanism to call "document.form1.var2.foo"
when I can call it directly!


You can, it's what square brackets are used for. If you want to
reference a form control using 'dot notation', use:

document.form1.var2;

where form1 and var2 are the names/ids of a form and form control
respectively. If you want to use a variable or some expression that
must be evaluated, then you must use square brackets:

var elName = 'var2';
document.form1[elName];

or

var elNameRoot = 'var';
document.form1[elNameRoot + '2'];

or more formally:

document.forms['form1'].elements[elNameRoot + '2'];
Dot notation restricts the characters that can be used in the name/id of
an element, e.g. it won't work with names that have dots or square
brackets in them. It also hard-codes element names/ids.
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
May 2 '06 #8
Now I didn't bother to read all the replies.
But wouldn't it be a heck of a lot easier to just use an Array?

var foo = new Array();

foo[x] = "moo";

function cowsSay(x);
{
alert(foo[x]);
}

Just a thought. :)

May 2 '06 #9
Zif
mt*******@gmail.com wrote:
Now I didn't bother to read all the replies.
Or even the question it seems. If you did, you missed the point.

If you'd quoted some part of the OP, you might have realised that the
question was about how to access elements with sequential numbering
using their name and the forms.elements collection - you know, the bit
in the OP with 'var1, var2, var3 etc.'.

But wouldn't it be a heck of a lot easier to just use an Array?


How? Your (wild) stab in the dark doesn't do it for me at least.

[...]

--
Zif
May 2 '06 #10

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

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
13
by: Deano | last post by:
Apparently you can only do this with one value i.e Call MyAssetLocationZoom(Me!txtLocation, "Amend data") This runs; Public Sub MyAssetLocationZoom(ctl As Control, formName As String) On...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.