Forum Moderators: open

Message Too Old, No Replies

Checkbox one item at a time.

not really working...

         

Mekazama

3:22 am on Jan 4, 2007 (gmt 0)

10+ Year Member



Hi,

My program is developed using Lotus Domino and I need a Javascript to control the checkbox where only one item is allowed to be selected at a time.Using radio btn will solve the problem but I've been told to do it by using a checkbox.The problem is that the element inside the checkbox is not fix(extracted from database)so the id for every element is the same.

here my javascript:
====================================================================
onClick="checkbox(this,<field id>)";

function checkbox(me,group)
{
var checked = me.checked;
var unchecked = me.unchecked;
if (checked) for (var i = 1; i < arguments.length; i++){
var ck = document.getElementById(arguments[i]);
if (ck) ck.checked = unchecked;
}
me.checked = checked;
}
=====================================================================

daveVk

5:33 am on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see a few issues here
- using checkboxs will confuse users.
- "var unchecked = me.unchecked;" should be "var unchecked!= me.checked;" actually probably not needed at all as "if (ck) ck.checked = unchecked;" can be simplified to "ck.checked = false;".
- if you have not got unique ids, consider using getElementsByName using name attribute instead of id.
- if this form is to be submitted, may have to sort out id issues.
- What should happen on unticking?

Mekazama

7:59 am on Jan 4, 2007 (gmt 0)

10+ Year Member



Thanks daveVk for ur opinion..

Instead of using getElementbyId method.. I'm referencing the field name itself..The checkbox now run correctly where only one item can be choosen at a time and it also can be untick..

//********************
function checkbox(me,group)
{
var checked = me.checked;

for (i = 0; i < arguments.length; i++) {
if(me){
document.forms[0].Field[i].checked=false;
}
}
me.checked = checked;
}

daveVk

1:50 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"if(me)" tests seems to be in odd position, if required place it around entire function boby? Traditional radio buttons dont allow you to untick, perhaps its ok in this application to leave none selected?