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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.