Page 1 of 2

My JavaScript functions are undefined (to FireFox's opinion)

Posted: Thu Sep 15, 2005 4:40 am
by pilau
Greetings.

Take a look at this webpage: http://www.wormiverse.com/rewclan/calendar.php

The checkboxes near month names are supposed to run the function "invertCheck".. however, it states that my function are not defined...

Can you see the problem in my code?

TIA

Posted: Thu Sep 15, 2005 4:57 am
by shiznatix
well i dunno if this is the problem becuase its probably perfectly valid but

javascript:invertCheck('December[]')

iv never seen the use of javascript:fuction_name. why not just try the onclick="invertCheck('December[]')" like you do at the bottom of the site

Posted: Thu Sep 15, 2005 7:08 am
by pilau
I did what you told me but the function is still undefined.

FireFox gave me this error:

Error: missing name after . operator
Source File: http://www.wormiverse.com/rewclan/calendar.php
Line: 21, Column: 34
Source Code:
if (document.elements.'+NAME+'.checked==true)

Please note that it won't recognize the NAME parameter AND the function (here's the functoin code:)

Code: Select all

function invertCheck(NAME)
{
var inps = document.getElementsByTagName('input');
   for (var x in inps)
   {
            if (document.elements.'+NAME+'[i].checked==true)
            {
     		 if (inps[x].type == 'checkbox' && inps[x].name == NAME) inps[x].checked = false;
	    }
            else if (document.elements.'+NAME+'[i].checked==false)
            {
                 if (inps[x].type == 'checkbox' && inps[x].name == NAME) inps[x].checked = true; 
            }

   }

}

Posted: Thu Sep 15, 2005 7:50 am
by CoderGoblin
My understanding is that you cannot use a string within code...

Code: Select all

if (document.elements.'+NAME+'[i].checked==true)
should be

Code: Select all

if (document.elements[NAME]][i].checked==true)
or at least something similar to it
Another alternative is to create a string of the whole command and eval it.

Code: Select all

function invertCheck(NAME)
{
     var inps = document.getElementsByTagName('input');
     var do_it="";
     for (var x in inps) {
          do_It="
              if (document.elements."+NAME+"[i].checked==true) {
                  if (inps[x].type == 'checkbox' && inps[x].name == "+NAME+") inps[x].checked = false;
              } else if (document.elements."+NAME+"[i].checked==false) {
                  if (inps[x].type == 'checkbox' && inps[x].name == "+NAME+") inps[x].checked = true;
              } ";
         eval(do_It);
    }
}

Posted: Thu Sep 15, 2005 7:54 am
by pilau
That's what I had in mind, using the EVAL function. But I still need to find out why the functions are not defined!

Posted: Thu Sep 15, 2005 7:55 am
by CoderGoblin
If you have a syntax error in the function, the whole function will be undefined.

Posted: Thu Sep 15, 2005 8:07 am
by pilau
Here's the entire JavaScript code for that page:

Code: Select all

function addFields(monthName) {
document.forms.container.action.value = "addfields";
document.forms.container.month.value = EVAL(monthName);
document.forms.container.submit();
}

function applyChanges() {
document.forms.container.action.value = "apply";
document.forms.container.submit();
}

function invertCheck(NAME)
{
     var inps = document.getElementsByTagName('input');
     var do_it="";
     for (var x in inps) {
          do_It="
              if (document.elements."+NAME+"[i].checked==true) {
                  if (inps[x].type == 'checkbox' && inps[x].name == "+NAME+") inps[x].checked = false;
              } else if (document.elements."+NAME+"[i].checked==false) {
                  if (inps[x].type == 'checkbox' && inps[x].name == "+NAME+") inps[x].checked = true;
              } ";
         eval(do_It);
    }
}
Now tell me if there's something wrong please.

Posted: Thu Sep 15, 2005 8:09 am
by feyd
eval() 'tis a waste, says I. Use CoderGoblin's alternate route, yar.

Posted: Thu Sep 15, 2005 8:18 am
by pilau
Practicing for Pirate's day, Feyd? ;)

I did what you said, and now FF's JavaScript console says:

Error: document.elements has no properties
Source File: http://www.wormiverse.com/rewclan/calendar.php
Line: 21

EDIT:

I fixed that and now it says:

Error: missing name after . operator
Source File: http://www.wormiverse.com/rewclan/calendar.php
Line: 21, Column: 34
Source Code:
if (document.elements.[NAME].checked==true)

I'm lost :(

Posted: Thu Sep 15, 2005 8:19 am
by raghavan20

Code: Select all

if (document.elements."+NAME+"[i].checked==true) {
I dont see this to be valid statement
how can quotes come in there???
where is the 'i' variable coming from???
its better if you do something like

Code: Select all

var condition = "document.elements."+NAME+ "[" + i + "].checked==true"
alert(condition);
if (condition)
check whether this code executes properly.

Posted: Thu Sep 15, 2005 8:22 am
by feyd
Rag, you're if'ing a string, 'tis always true unless 'tis empty..

yar.

Posted: Thu Sep 15, 2005 8:31 am
by pilau
I just got to the fact that the For loop won't run only on the array elemnts but it would be infinite - I just reached the 100 alert in December's loop (should only be 3)

And I can't quit my FireFox.

Could anybody tell me why is this For loop infinite?

Code: Select all

var inps = document.getElementsByTagName('input');
   for (var i in inps)

Posted: Thu Sep 15, 2005 8:38 am
by CoderGoblin
Normally for for loops you would use...

Code: Select all

for (var i=0; i<inps.length; i++) {
 // use of name or whatever is inps[i].name
}
I would recommend while testing to place a break in your code...

Code: Select all

for (var i=0; i<inps.length; i++) {
  // use of name or whatever is inps[i].name
  if (i>1000) {
   alert('Too many loops in function xyz');
   break;
  }
}
or alternatively throw an exception.

Posted: Thu Sep 15, 2005 8:47 am
by pilau
I'm getting this error now:

Error: missing name after . operator
Source File: http://www.wormiverse.com/rewclan/calendar.php
Line: 21, Column: 34
Source Code:
if (document.elements.[NAME].checked==true)

Here's the code:

Code: Select all

function invertCheck(NAME)
{
var inps = document.getElementsByTagName('input');
   for (var i=0; i<inps.length; i++)
   {
            if (document.elements.[NAME][i].checked==true)
            {
            if (inps[i].type == 'checkbox' && inps[i].name == NAME) inps[i].checked = false;
            }
            else if (document.elements.[NAME][i].checked==false)
            {
                 if (inps[i].type == 'checkbox' && inps[i].name == NAME) inps[i].checked = true;
            }

/*var condition = "document.elements."+NAME+ "[" + i + "].checked==true"
alert(condition);
if (i > 10) {break;}*/
   }

}

Posted: Thu Sep 15, 2005 8:56 am
by raghavan20

Code: Select all

if (document.elements.[NAME][i].checked==true)
I think you can not put NAME in square brackets since its an identifier not an array index.