How to get Variable value not Variable name.

P

Pete Mahoney

I am currently working on an ASP page where I create a lot of
different check boxes. I have some checkboxes that are Windows
platforms and some that are solaris platforms. I want a control
checkbox over the Windows ones to turn them all on or all off, and the
same with the solaris.

One of the control check boxes looks something like this:

<input type="checkbox" name="objRS("ProductId") & "_solaris"
value=""0"" onClick=""checkAll(this.form,name);"">

This gives me a check box with a name that looks like:

194_solaris

194 is the productID for a product at my company, and solaris is just
to tell if its windows or solaris. Now I know this is the name of
that checkbox this is NOT my problem.

Now when I pass the onClick=""checkAll(this.form,name) part I go to:

<SCRIPT LANGUAGE="JavaScript">
function checkAll(form, CheckBoxName)
var myVar = CheckBoxName
if (form.CheckBoxName.checked == true){ <-------- Problem Line
for (i=0; i<form.elements.length; i++)
{
if (form.elements.id == myVar)
form.elements.checked = true;
}
}else {
for (i=0; i<form.elements.length; i++)
{
if (form.elements.id == myVar)
form.elements.checked = false;
}
</script>

Now when I call this I get an error thats says:

"CheckBoxName.checked is null or not declared"

Well I know good and well CheckBoxName.checked is not declared, but
the VALUE of CheckBoxName is a valid name. I want the content of the
variable named CheckBoxName not the name. How can I do this?
 
D

Dan Brussee

I am currently working on an ASP page where I create a lot of
different check boxes. I have some checkboxes that are Windows
platforms and some that are solaris platforms. I want a control
checkbox over the Windows ones to turn them all on or all off, and the
same with the solaris.

One of the control check boxes looks something like this:

<input type="checkbox" name="objRS("ProductId") & "_solaris"
value=""0"" onClick=""checkAll(this.form,name);"">

This gives me a check box with a name that looks like:

194_solaris

194 is the productID for a product at my company, and solaris is just
to tell if its windows or solaris. Now I know this is the name of
that checkbox this is NOT my problem.

Now when I pass the onClick=""checkAll(this.form,name) part I go to:

<SCRIPT LANGUAGE="JavaScript">
function checkAll(form, CheckBoxName)
var myVar = CheckBoxName
if (form.CheckBoxName.checked == true){ <-------- Problem Line
for (i=0; i<form.elements.length; i++)
{
"CheckBoxName.checked is null or not declared"

Well I know good and well CheckBoxName.checked is not declared, but
the VALUE of CheckBoxName is a valid name. I want the content of the
variable named CheckBoxName not the name. How can I do this?

a. I would try not to use the word "form" as a variable name. Too easy
to get mixed up with the object type. But, if it works - fine.

if (form [CheckBoxName].checked == true) {

That should do it for you. Make sure there is a space between the form
object and the first square bracket.
 
Y

YD

Pete said:
[...]
Now when I pass the onClick=""checkAll(this.form,name) part I go to:

You may pass a reference to the checkbox as argument of the JS function :
onclick="checkAll(this)"

and modify the function that way :

function checkAll(checkbox){
var myForm=checkbox.form;
if(checkbox.checked) /* no need to compare to true
(if checkbox is checked your statement is if(true==true), if(true) is enough! */
//etc.
}

HTH
 
P

Peter Landerud

This also does not work. I get an error what says:

Error, 'checked' is null or not an object

Check is a part of any checkbox is HTML and I don't know what this
means. Please help.
 
R

Richard Cornford

<SCRIPT LANGUAGE="JavaScript">
function checkAll(form, CheckBoxName)
var myVar = CheckBoxName
if (form.CheckBoxName.checked == true){
for (i=0; i<form.elements.length; i++)
{
if (form.elements.id == myVar)
form.elements.checked = true;
}
}else {
for (i=0; i<form.elements.length; i++)
{
if (form.elements.id == myVar)
form.elements.checked = false;
}
</script>

Now when I call this I get an error thats says:

"CheckBoxName.checked is null or not declared"

<snip>

No it doesn't. The message states that "form.checkNoxName is null or not
an object" ( or local translation). If you are going to state error
messages it would be a good idea to _read_ the message first.

See:-

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

And read the linked article (sq_brackets.html).

Richard.
 
P

Peter Landerud

This still does not work. I get an error that says:

Error, 'Checked' is null or not an object

Checkboxes in HTML have a checked option, why does it think this is an
object and not a method? Please help.
 
P

Peter Landerud

Hi Richard,

Actually the error message said exactally that. I have tryed the []
thing and it still gives me that same error. Any idea why this is
happening?

I tryed this (myform [CheckName].checked)

I am 100% sure I have a checkbox with that name, and wonder why I keep
getting this error.
 
D

Dan Brussee


I must admit I do not have documentation to show why I say that, but I
do know that I always put the space there and it always works. I have
seen code that did not work without it, but I cannot give you the
reasons why it did not. I suppose I should find this out some day :)
 
R

Richard Cornford

I must admit I do not have documentation to show why I
say that, but I do know that I always put the space there
and it always works. I have seen code that did not work
without it, but I cannot give you the reasons why it did
not. I suppose I should find this out some day :)

JavaScript is very tolerant of white space but I think that if you have
seen code that did not work without the space then the space or
otherwise was not the cause. I never include spaces in square bracket
property accessors (I think that it would make the code obscure) and I
have never had a problem doing so.

Richard.
 
R

Richard Cornford

Peter Landerud said:
Actually the error message said exactally that. I
have tryed the [] thing and it still gives me that
same error. Any idea why this is happening?

I tryed this (myform [CheckName].checked)

I am 100% sure I have a checkbox with that name, and
wonder why I keep getting this error.

There is nothing in your script that treats - CheckBoxName.checked - as
if it was an object so your browser should not even be attempting to
resolve it as an object reference.

However, I did notice that you have more opening braces ( - { - ) than
closing so it might be that you are using IE to test and suffering an
unusual side effect of its excessive error correcting behaviour.
Generally Gecko browsers (Mozilla, etc.) and Opera 7 give much better
error reports and do not even attempt to second guess erroneous
JavaScript.

Richard.
 
R

Richard Cornford

Incidentally, the current HTML specifications forbid the strings in name
and ID attributes from starting with a number so your "194_solaris" name
attribute is invalid. While most browsers don't seem to care about this
it is not realistic to expect invalid HTML to result in a consistent or
usable DOM or to expect all browsers to react in the same way to invalid
code.

Richard.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top