My JavaScript functions are undefined (to FireFox's opinion)
Moderator: General Moderators
My JavaScript functions are undefined (to FireFox's opinion)
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
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
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:)
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;
}
}
}- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
My understanding is that you cannot use a string within code...
should be
or at least something similar to it
Another alternative is to create a string of the whole command and eval it.
Code: Select all
if (document.elements.'+NAME+'[i].checked==true)Code: Select all
if (document.elements[NAME]][i].checked==true)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.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Here's the entire JavaScript code for that page:
Now tell me if there's something wrong please.
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);
}
}
Last edited by pilau on Thu Sep 15, 2005 8:10 am, edited 1 time in total.
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
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.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Code: Select all
if (document.elements."+NAME+"[i].checked==true) {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)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?
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)- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Normally for for loops you would use...
I would recommend while testing to place a break in your code...
or alternatively throw an exception.
Code: Select all
for (var i=0; i<inps.length; i++) {
// use of name or whatever is inps[i].name
}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;
}
}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:
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;}*/
}
}- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Code: Select all
if (document.elements.[NAME][i].checked==true)