Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Has checkbox been checked or unchecked?

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
I am trying to determine if the user has checked or unchecked a checkbox, the following code always seems to be running the else condition:

var mssge
function testing(catId)
{
for (i=0; i < eventCategories.length; ++i)
{
if (eventCategories.checked)
{
mssge = ("you have checked : " + catId);
}
else
{
mssge = ("you have unChecked : " + catId);
}

}
alert (mssge);
}


<input type="checkbox" name="eventCategories" onClick="testing(<%= intVal %>);">

TYIA your help is appreciated
 
Code:
function testing(catId)
{
  for (i=0; i < [red]document.forms['myfrm'].elements['eventCategories'][/red].length; ++i)
       {
          if ([red]document.forms['myfrm'].elements['eventCategories'][/red][i].checked)
          {
             mssge = ("you have checked : " + catId);  
          }
          else
          {
             mssge = ("you have unChecked : " + catId);
          }
          
       }
       alert (mssge);
    }
Does this work?

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
chessbot

Thank you for prompt reply. Unfortunatly no, your function seems to always run the else condition also.

This is my latest attempt, this one always seems to always run the if condition:

var mssge
var checked = false;
function testing(catId)
{
for (var i=0; i < editEvent.eventCategories.length; i++)
{
if(editEvent.eventCategories.checked == true)
{
checked = true;
mssge = ("you have checked : " + catId);
break;
}
else if(!checked)
{
mssge = ("you have unChecked : " + catId);
}

}
alert (mssge);
}


Any help with this seemingly strait forward objective would be very appreciated. I have turned to google now, there is loads on validation however nothing so far on my problem
 
I would rewrite it this way:

Code:
function testing(catId)
{
var mssge = 'unChecked';
for (var i=0; i < editEvent.eventCategories.length; i++)
  {
  if(editEvent.eventCategories[i].checked)
    {
    mssge = 'checked';
    break;  
    }
  }
alert('You have ' + mssge + ': ' + catId);
}

Lee
 
trollacious, thank you for your reply - a much tidier function. You may be able to tell my JS is average, I am primarily an ASP guy.

Your function seems to also return 'you have checked : [int]' everytime. When I uncheck it I am trying to return 'you have unChecked : [int]'
 
Okay, let's mix what chessbot wrote (he's pretty good, and has helped a LOT of people) with what I did from what I copied of your last attempt:

Code:
function testing(catId)
{
var mssge = 'unChecked';

for (i=0; i < document.forms['myfrm'].elements['eventCategories'].length; ++i)
  {
  if (document.forms['myfrm'].elements['eventCategories'][i].checked)
    {
    mssge = 'checked';
    }
  }
alert('You have ' + mssge + ': ' + catId);
}

Your best bet is to use the associative array format like he did. I was sloppy and just copied the code you'd used rather than really reading what had been written.

Or, you could even write it like this:

Code:
<input type="checkbox" name="eventCategories" onClick="testing(this,<%= intVal %>);">

function testing(oneelement, catId)
{
var status = 'unChecked';

if (oneelement.checked)
  {
  status = 'checked';
  }

alert('You have ' + status + ': ' + catId);
}

Lee
 
Are you sure that there is more that one check box named "eventCategories"? If there is only one, the code will break on "document.forms[...].elements[...].length".




P.S. Thanks, Lee

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
I wondered about that, too, or if mat41 was using another name for one of the checkboxes he was testing. The second example I wrote is more flexible that way, though only indicates the status of the checkbox clicked on, where looping through the array finds the first one checked and reports that.

As well, you didn't get to be so high up on the list of experts here without helping a lot of people. :)# (bearded smiley face)

Lee
 
Double post, apparently.

mat41, try trollacious's (umm, possessive?) second (or third, if you think about it that way).

P.S. Do you happen to know how the MVP list is calculated? cLFlaVa and I were speculating on it in another thread but without resolution. It seems to have something to do with frequency of posts.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Wow, great it works!!

trollacious
What is the associative array format, the way you have refered to the DOM (.forms & .elements)?

chessbot
;;;Are you sure that there is more that one check box named "eventCategories"?

Yes, there will always be more than one

To wrap up things thank you once agian. I havnt posted on this forum often, last time Pittiman exceeded all expectations with his brilliance. I will be sure to contribute to the ASP area id you have one, I am primarily a p2p.wrox.com guy

You guys have a damn fine day!
matt
 
You mean my LAST attempt. :)#

In the second to last code I wrote, the ++i in the for loop declaration should be i++, too.

I haven't been around for a couple weeks, out of town visiting family, and I'm still up there on the list for JS. That has me confused a LOT. I posted a few years ago when they didn't require membership and ended up on the MVP list, but then the rules changed and I couldn't post any more without becoming a member. My name stayed on the list for several weeks after that, which I thought was amusing. I was reluctant to become a member because I didn't want to get any garbage mail, and so far haven't had more than a half a dozen pieces from this place. I suppose that's the price of free membership, though. :)#

Lee
 
Each element in a form, and the form itself, has a name. In Javascript, an array can be created with elements having names, or in numerical order. From what I understand, some of the more recent browsers require that the form and its elements be referred to by their names, and this is what associative means. Since the elements on the page aren't REALLY listed in any numerical order, it's possible that a browser could be designed where the text inputs were ordered first, then the checkboxes, then radio buttons, then textareas, and whatever else is used as an element. The same with forms. The forms could be stored in memory in alphabetical order rather than the order they are on the page. So, document.forms[0] wouldn't necessarily be the same in every browser, if there was more than one form, but document.forms['registration'] would be.

Clear as mud now?

Lee
 
trollacious

;;;You mean my LAST attempt. :)#
Number three : ('Or, you could even write it like this')

I picked up the ++i

;;;Clear as mud now?
Crystal - Thank you kindly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top