Why does this cause an Obect required message? It seems to work...

I

Iver Erling Årva

I have the following function which I call from a <input type="button">'s onclick event:

function showHideSub(cntrl,varclass){
var myclass = varclass;
DIVColl=document.all.tags("tr");
if (cntrl.value == "+"){
cntrl.value = "-";
for (i=0; DIVColl.length; i++){
if ((DIVColl(i).className) == myclass){
DIVColl(i).style.display="";
}
}
}
else {
cntrl.value = "+";
for (i=0; DIVColl.length; i++){
if (DIVColl(i).className == myclass){
DIVColl(i).style.display="none";
}
}
}
}

I use this function to show/hide an extra line of details under some invoice charge items, so by hitting the + button, the details are displayed and + is changed to -. By hitting it again the extra line is hidden and the - is changed back to + and so on.

Now, this seem to work, but every time I hit the button and the function has done it's job, I get an error message saying: Requires Object. The line it crashes on is if ((DIVColl(i).className) == myclass){
I cannot understand why it does this? In this case i is 19 and DIVColl.length is 19 too, but if I change it to for (i=0; DIVColl.length-1; i++){ I get the same error although i then is 18.

Any help would be most appreciated!

Brgds
(e-mail address removed)
 
M

Michael Winter

First thing: please don't post using HTML to non-binary newsgroups. Some
news readers can't handle that form of data.
I have the following function which I call from a <input
type="button">'s onclick event:

function showHideSub(cntrl,varclass){
var myclass = varclass;

I don't see the point in that assignment. Either change the instances of
'myclass' below to 'varclass', or 'varclass' above to 'myclass'.
DIVColl=document.all.tags("tr");

You should declare DIVColl with the var keyword otherwise it will become a
global variable. Also, the document.all and all.tags properties are
proprietary Microsoft code. If this is for the Web, you shouldn't use them
exclusively. If it's for personal use or a controlled environment (a LAN,
for instance), then don't worry.

Just so you know, a more Web-friendly alternative would be:

var divColl = [];

if(document.getElementsByTagName) {
divColl = document.getElementsByTagName('TR');
} else if(document.all && document.all.tags) {
divColl = document.all.tags('TR') || [];
}
if (cntrl.value == "+"){
cntrl.value = "-";
for (i=0; DIVColl.length; i++){

Again, 'i' should be declared with var so it doesn't become global.

This statement is also where the problem lies.

The second part of the for statement is an expression that is evaluated at
the end of each loop. If the expression is true, the loop is executed
again, and terminated if the expression is false. The only way that the
expression, DIVColl.length, will be evaluated is false is if the
collection contains no elements (a length of zero), in which case the loop
won't execute at all. What you want is:

for(var i = 0; i < DIVColl.length; i++) {

or

for(var i = 0, n = DIVColl.length; i < n; ++i) {

The second version just prevents continuous property look-ups.
if ((DIVColl(i).className) == myclass){

This, just like document.all and all.tags, is also proprietary Microsoft
code. Collections should be subscripted (indexed) with brackets [] not
parentheses ().
DIVColl(i).style.display="";

Same here. If this is for the Web, you should also test that the style
object is supported. See the group's FAQ notes for information on feature
detection:

<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>

The remarks above apply to the code in the else block, too.

Hope that helps,
Mike
 
I

Iver Erling Årva

Needless to say that did the trick!

Thank you ever so much for taking so much time to help me with this. I am
really grateful! I am from time to time struggling a bit with the
intricacies of JS. I am working with a database language called Caché making
a web application. By now I am able to figure most things out by myself
fortunately, but from time to time I'm really banging my head in the wall,
so this newsgroup and people like yourself are invaluable for a person like
me who don't have anybody else to ask.

Best regards
Iver Erling Aarva, Oslo, Norway


Michael Winter said:
First thing: please don't post using HTML to non-binary newsgroups. Some
news readers can't handle that form of data.
I have the following function which I call from a <input
type="button">'s onclick event:

function showHideSub(cntrl,varclass){
var myclass = varclass;

I don't see the point in that assignment. Either change the instances of
'myclass' below to 'varclass', or 'varclass' above to 'myclass'.
DIVColl=document.all.tags("tr");

You should declare DIVColl with the var keyword otherwise it will become a
global variable. Also, the document.all and all.tags properties are
proprietary Microsoft code. If this is for the Web, you shouldn't use them
exclusively. If it's for personal use or a controlled environment (a LAN,
for instance), then don't worry.

Just so you know, a more Web-friendly alternative would be:

var divColl = [];

if(document.getElementsByTagName) {
divColl = document.getElementsByTagName('TR');
} else if(document.all && document.all.tags) {
divColl = document.all.tags('TR') || [];
}
if (cntrl.value == "+"){
cntrl.value = "-";
for (i=0; DIVColl.length; i++){

Again, 'i' should be declared with var so it doesn't become global.

This statement is also where the problem lies.

The second part of the for statement is an expression that is evaluated at
the end of each loop. If the expression is true, the loop is executed
again, and terminated if the expression is false. The only way that the
expression, DIVColl.length, will be evaluated is false is if the
collection contains no elements (a length of zero), in which case the loop
won't execute at all. What you want is:

for(var i = 0; i < DIVColl.length; i++) {

or

for(var i = 0, n = DIVColl.length; i < n; ++i) {

The second version just prevents continuous property look-ups.
if ((DIVColl(i).className) == myclass){

This, just like document.all and all.tags, is also proprietary Microsoft
code. Collections should be subscripted (indexed) with brackets [] not
parentheses ().
DIVColl(i).style.display="";

Same here. If this is for the Web, you should also test that the style
object is supported. See the group's FAQ notes for information on feature
detection:

<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>

The remarks above apply to the code in the else block, too.

Hope that helps,
Mike
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top