...

View Full Version : Has checkbox been checked or unchecked?



mat41
12-08-2004, 03:30 AM
I am trying to determine if the user has checked or unchecked a checkbox, the following code always seems to be running the if condition:
------------------------------------------
var mssge
var checked = false;
function testing(catId)
{
for (var i=0; i < editEvent.eventCategories.length; i++)
{
if(editEvent.eventCategories[i].checked == true)
{
checked = true;
mssge = ("you have checked : " + catId);
break;
}
else if(!checked)
{
mssge = ("you have unChecked : " + catId);
}

}
alert (mssge);
}


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

Please note : There are multiple checkboxes with the same name. The way the page is set up I would like to post a commer delimited string to the destination page (All the checkboxes have dynamic values)

I have been at this all morning. I am starting to believe my objective isnt all that strait forward, of course of they had unique names it would be.

I am looing forward to finding out if this is possible

TYIA your help is appreciated

Brandoe85
12-08-2004, 04:09 AM
When your referencing the checkbox you need document.editEvent.eventCategories.length. Change them lines and see what you get...

mat41
12-08-2004, 04:33 AM
Brandoe85

Thank you for your reply. I believe the 'document' part of the DOM is optional, this makes no difference to the function. Another version of it that fails to work:

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

Brandoe85
12-08-2004, 04:55 AM
Odd...it fills my javascript console with errors without the document...

glenngv
12-08-2004, 05:10 AM
I believe the 'document' part of the DOM is optional, this makes no difference to the function.
No. IE just makes all named and id'ed elements as global references so directly accessing a form by its name work fine.

But if you use Firefox and other browsers, it will throw an error if 'document' is not specified. That behavior is correct because that's the proper way of accessing elements.

mat41
12-08-2004, 05:13 AM
Yes, very odd. I am obviously not a JS expert, I have been lead to believe it is optional would be a more accurate thing to say. This is where a moderator comes in handy.

Anyhow the solution:

<input type="checkbox" onClick="testing(this,<%= getSecond(0) %>);"...

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

if (oneelement.checked)
{
status = 'checked';
alert('You have ' + status + ': ' + catId);
}
else
{
alert('You have ' + status + ': ' + catId);
}

Yes, I'm still freaking out about thee not being a for loop in there. The problem got solved at:
http://www.tek-tips.com/viewthread.cfm?qid=964029&page=1

Have a fine day

mat41
12-08-2004, 05:21 AM
glenngv

Ahhhh it all becomes clear. Thank you, a useful piece of information.

All
I hope it is not inappropriate placing the link to tek-tips where the solution came from. Also, I am not usually a person who posts the same question to multiple forums, this one I thought was not do-able (since the checkboxes all had the same name) I felt this way I would get a solution quicker

TYVM



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum