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

newby question joining variables and strings together

P: n/a
im pretty ne to java script - so please be gentle :)

basically what i want to do is construct an action in a function using a
variable sent to that function
ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}

i have tried all sorts but cnt seem to get the result i want - any advice
would be great

Sep 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

chris wrote:
im pretty ne to java script - so please be gentle :)

basically what i want to do is construct an action in a function using a
variable sent to that function
ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}

i have tried all sorts but cnt seem to get the result i want - any advice
would be great


I believe the effect you want to achieve is the following:

function myFunc(btnNum)
{
document.forms["formName"].elements["button" + btnNum].disabled =
true;
}

Hope this helps. :)

Sep 21 '05 #2

P: n/a
Lee
chris said:

im pretty ne to java script - so please be gentle :)
It's Javascript, not "java script". The distinction is important
because otherwise people get the impression that it is somehow
related to Java.

basically what i want to do is construct an action in a function using a
variable sent to that function

ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}

i have tried all sorts but cnt seem to get the result i want - any advice
would be great


form1.button34.disabled is not a string. It's a reference in "dot notation",
and may not include variables. To do what you want, you need to use "square
bracket notation". You should also always refer to forms in relation to the
document, not as if the form reference was a global variable:

function disableButton(n) {
document.form1.elements["button"+n].disabled=true;
}

Sep 21 '05 #3

P: n/a
Thanks All, just wat i needed to know
--

"Lee" <RE**************@cox.net> wrote in message
news:dg*********@drn.newsguy.com...
chris said:

im pretty ne to java script - so please be gentle :)


It's Javascript, not "java script". The distinction is important
because otherwise people get the impression that it is somehow
related to Java.

basically what i want to do is construct an action in a function using a
variable sent to that function

ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}

i have tried all sorts but cnt seem to get the result i want - any advice
would be great


form1.button34.disabled is not a string. It's a reference in "dot
notation",
and may not include variables. To do what you want, you need to use
"square
bracket notation". You should also always refer to forms in relation to
the
document, not as if the form reference was a global variable:

function disableButton(n) {
document.form1.elements["button"+n].disabled=true;
}

Sep 22 '05 #4

P: n/a
["Followup-To:" header set to alt.comp.lang.javascript.]
On 2005-09-21, chris <so*****@here.com> wrote:
im pretty ne to java script - so please be gentle :)

basically what i want to do is construct an action in a function using a
variable sent to that function
ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}


this appears to be what you are trying to di,
it may work (I have not tested it).
but it is non-standard and will cause warnings in mozilla, and may not at all in
other browsers.

function ($number)
{
eval("form1.button"+$number+".disabled=true;");
}

you should be using document.GetElementById

this means giving any element you want to refer to an id tag.
that means you can have non-submitted form inputs by not giving
them a name tag.

Bye.
Jasen
Oct 16 '05 #5

P: n/a
Lee
Jasen Betts said:

["Followup-To:" header set to alt.comp.lang.javascript.]
On 2005-09-21, chris <so*****@here.com> wrote:
im pretty ne to java script - so please be gentle :)

basically what i want to do is construct an action in a function using a
variable sent to that function
ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}


this appears to be what you are trying to di,
it may work (I have not tested it).
but it is non-standard and will cause warnings in mozilla, and may not at all in
other browsers.

function ($number)
{
eval("form1.button"+$number+".disabled=true;");
}

you should be using document.GetElementById

this means giving any element you want to refer to an id tag.
that means you can have non-submitted form inputs by not giving
them a name tag.


You should never use eval() to access a form element, and using
getElementById() is usually almost as bad. Use:

document.form1.elements["button"+$number].disabled=true;

Oct 16 '05 #6

P: n/a
Jasen Betts said the following on 10/15/2005 8:48 PM:

["Followup-To:" header set back to the legitimate comp.lang.javascript.]
["Followup-To:" header set to alt.comp.lang.javascript.]
On 2005-09-21, chris <so*****@here.com> wrote:
im pretty ne to java script - so please be gentle :)

basically what i want to do is construct an action in a function using a
variable sent to that function
ok - what i want to do is

function ($number)
{
form1.button[add in the $number here].disabled=true;
}

say $number was 34, so in effect i would end up with

function ($number)
{
form1.button34.disabled=true;
}
this appears to be what you are trying to di,
it may work (I have not tested it).
but it is non-standard and will cause warnings in mozilla, and may not at all in
other browsers.

function ($number)
{
eval("form1.button"+$number+".disabled=true;");
}


As Lee pointed out, NEVER use eval to access form elements.
you should be using document.GetElementById
syntax error. It is document.getElementById
this means giving any element you want to refer to an id tag.
that means you can have non-submitted form inputs by not giving
them a name tag.


Or, you get both by giving it a name *attribute*, then use the forms
collection to access it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.