Jump to content

[SOLVED] Why does this happen?


lalabored

Recommended Posts

When I use this code, it works fine...

 

var checkflag = "false";

function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}

 

However, when I use this...

 

var checkflag = "false";

 

function checkAll(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}

 

It doesn't work. Is there something about Javascript that does this?

Link to comment
https://forums.phpfreaks.com/topic/56001-solved-why-does-this-happen/
Share on other sites

make sure you call the right function check or checkAll

 

Yes, I checked that, this is what I used.

This works...

 

<script type="text/javascript">

var checkflag = "false";

function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
</script>

<input type="button" onclick="this.value = check(document.getElementsByName('pm[]'));" value="Check All">

 

This doesn't work...

 

<script type="text/javascript">

var checkflag = "false";

function checkAll(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
</script>

<input type="button" onclick="this.value = checkAll(document.getElementsByName('pm[]'));" value="Check All">

 

I don't see any difference yet it doesn't work, I don't understand why... I'm using Firefox if that helps.

both these scripts work fine, make sure you don't have an error somewhere else in your code that stops the script from working. In firefox, after you get the error click on "Tools" from the top menu (next to File, Edit, View) and select "Error Console" (or Javascript Console if you have an older version).

 

This should let you know where is your error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.