newby question joining variables and strings together

C

chris

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
 
W

web.dev

chris said:
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. :)
 
L

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;
}
 
J

Jasen Betts

["Followup-To:" header set to alt.comp.lang.javascript.]
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
 
L

Lee

Jasen Betts said:
["Followup-To:" header set to alt.comp.lang.javascript.]
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;
 
R

Randy Webb

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.]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top