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

using var syntax problem

P: n/a
Hi All,

Newbie here needing some syntax assistance. Here is the code:

function fnSelectOption(fieldname)
{
document.mainform. + fieldname + .value = "value";
}

It obvliously bombs where I am trying to ref the param "fieldname". I
realize it is a simple syntax issue but I cannot seem to figure it out. Any
help is greatly appreicated.

JC


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
<script language="jscript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}

</script>
</head>

<body>
<form name="mainform" >
<input type="text" name="text1" width="22">
<input type="button" value="Click Me" onclick="fnSelectOption('text1')";>
</form>

</body>

"DakotaCom" <jc******@rivervalleyit.com> wrote in message news:3f********@corp.newsgroups.com...
Hi All,

Newbie here needing some syntax assistance. Here is the code:

function fnSelectOption(fieldname)
{
document.mainform. + fieldname + .value = "value";
}

It obvliously bombs where I am trying to ref the param "fieldname". I
realize it is a simple syntax issue but I cannot seem to figure it out. Any
help is greatly appreicated.

JC


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Jul 20 '05 #2

P: n/a
MikeB wrote:
<script language="jscript">
No need for jscript here, just use:
<script type="text/javascript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}


This should be:
document.forms["mainform"][fieldname].value = "value";

(document.forms is not a function and you will get errors in other browsers
than IE)
JW

Jul 20 '05 #3

P: n/a
"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:R8********************@comcast.com...
<script language="jscript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}

<snip>

An obvious alternative to using IE specific code and shielding it form
non-IE browsers is by specifying the language attribute as jscript would
be to use the standard javascript/ECMA Script square bracket notation
property accessor syntax. Which is also fully supported by JScript:-

document.forms['mainform'][fieldname].value = "value";
-or-
document.forms['mainform'].elements[fieldname].value = "value";
-or-
document.forms.mainform.elements[fieldname].value = "value";

<URL: http://www.jibbering.com/faq/#FAQ4_39 >

Richard.
Jul 20 '05 #4

P: n/a
Thanks for the reponses! This seems to work for what I need, it also seems
to be the cleanest:

document.mainform[fieldname].value = "value";

JC
"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:R8********************@comcast.com...
<script language="jscript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}

</script>
</head>

<body>
<form name="mainform" >
<input type="text" name="text1" width="22">
<input type="button" value="Click Me" onclick="fnSelectOption('text1')";>
</form>

</body>

"DakotaCom" <jc******@rivervalleyit.com> wrote in message

news:3f********@corp.newsgroups.com...
Hi All,

Newbie here needing some syntax assistance. Here is the code:

function fnSelectOption(fieldname)
{
document.mainform. + fieldname + .value = "value";
}

It obvliously bombs where I am trying to ref the param "fieldname". I
realize it is a simple syntax issue but I cannot seem to figure it out. Any help is greatly appreicated.

JC


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #5

P: n/a
Thanks for the reponses! This seems to work for what I need, it also seems
to be the cleanest:

document.mainform[fieldname].value = "value";

JC
"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:R8********************@comcast.com...
<script language="jscript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}

</script>
</head>

<body>
<form name="mainform" >
<input type="text" name="text1" width="22">
<input type="button" value="Click Me" onclick="fnSelectOption('text1')";>
</form>

</body>

"DakotaCom" <jc******@rivervalleyit.com> wrote in message

news:3f********@corp.newsgroups.com...
Hi All,

Newbie here needing some syntax assistance. Here is the code:

function fnSelectOption(fieldname)
{
document.mainform. + fieldname + .value = "value";
}

It obvliously bombs where I am trying to ref the param "fieldname". I
realize it is a simple syntax issue but I cannot seem to figure it out. Any help is greatly appreicated.

JC


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #6

P: n/a
"DakotaCom" <jc******@rivervalleyit.com> writes:

Please don't top post, or at least trim your quotes.
Thanks for the reponses! This seems to work for what I need, it also seems
to be the cleanest:

document.mainform[fieldname].value = "value";


"*Seems* to work" is correct, at least for the browser you used.
It fails in, e.g., Mozilla/Netscape, because they don't make the form
a property of the document element.

Use
document.forms['mainform'] (or the equivalent: document.forms.mainform)
instead of just
document.mainform
That is the minimum you can write that works in all Javascript enabled
browsers.

Personally I prefer to use the "elements" collection, so it is
document.forms['mainform'].elements[fieldname].value = "value"
I think it is most consistent.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7

P: n/a
How come you guys always answer AFTER you've had your coffee.... ;-)

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bs*******************@news.demon.co.uk...
"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:R8********************@comcast.com...
<script language="jscript">
function fnSelectOption(fieldname)
{
// mainform is referenced in the forms collection and
// fieldname is referenced by name as a form element.
document.forms("mainform")(fieldname).value = "value";
}

<snip>

An obvious alternative to using IE specific code and shielding it form
non-IE browsers is by specifying the language attribute as jscript would
be to use the standard javascript/ECMA Script square bracket notation
property accessor syntax. Which is also fully supported by JScript:-

document.forms['mainform'][fieldname].value = "value";
-or-
document.forms['mainform'].elements[fieldname].value = "value";
-or-
document.forms.mainform.elements[fieldname].value = "value";

<URL: http://www.jibbering.com/faq/#FAQ4_39 >

Richard.

Jul 20 '05 #8

P: n/a
Lasse Reichstein Nielsen wrote:
"DakotaCom" <jc******@rivervalleyit.com> writes:
Thanks for the reponses! This seems to work for what I need, it also seems
to be the cleanest:

document.mainform[fieldname].value = "value";
"*Seems* to work" is correct, at least for the browser you used.
It fails in, e.g., Mozilla/Netscape, because they don't make the form
a property of the document element.


WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
Gecko/2004010908.

But I agree to the rest of your statements because of
[example using document.forms]
That is the minimum you can write that works in all Javascript enabled
browsers.
[...]

PointedEars
Jul 20 '05 #9

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
Gecko/2004010908.


Mozilla makes the form a property of the document if it is named using
the name attribute, and not if it is named only using the id attribute.
In the example I used (as most of the code I write) I only used id.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
Gecko/2004010908.
Mozilla makes the form a property of the document if it is named using
the name attribute, and not if it is named only using the id attribute.


True. Interestingly, it makes form elements a property of the form
object even if only the ID attribute is used.
In the example I used (as most of the code I write) I only used id.


You know, my magic Crystal Ball[tm] is still on holiday :)

Besides, you know that you become incompatible with older browsers
when using only the ID attribute for naming elememts but also access
them through collections/arrays?
PointedEars
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.