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

JavaScript and client side scripting.

Moderator: General Moderators

pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

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

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

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

   }

}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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);
    }
}
Last edited by CoderGoblin on Thu Sep 15, 2005 7:54 am, edited 1 time in total.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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!
Last edited by pilau on Thu Sep 15, 2005 8:01 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you have a syntax error in the function, the whole function will be undefined.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
Last edited by pilau on Thu Sep 15, 2005 8:10 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

eval() 'tis a waste, says I. Use CoderGoblin's alternate route, yar.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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 :(
Last edited by pilau on Thu Sep 15, 2005 8:20 am, edited 1 time in total.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Rag, you're if'ing a string, 'tis always true unless 'tis empty..

yar.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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;}*/
   }

}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
Post Reply