473,416 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Weird checkbox behavior (looks disabled but can still click on it)

I have two radio buttons and two checkboxes in a form.

I'm trying to write some code so that when a radio button is selected,
its corresponding checkbox is disabled.

My code looks like this:

function radioClicked(index)
{
document.table_config_form.my_checkbox[index].disabled = true;
}

function radioChanged(index)
{
document.table_config_form.my_checkbox[index].disabled = false;
}
The radios' onClick calls radioClicked() with the appropriate index.
The radios' onChange calls radioChanged() with the appropriate index.

It works fine in every respect except one: When the checkbox is
"disabled", it looks grayed out. However, when the user clicks on the
"disabled" checkbox, it behaves just like an enable checkbox! It
immediately goes back to white and a check appears in the checkbox.

If I eliminate the radioChanged() function, however, the disabled
checkboxes behave propertly.

I'm using IE 6.0 on W2K.

Many thanks for any suggestions,

David
Jul 20 '05 #1
1 6195
David Wake wrote:
I have two radio buttons and two checkboxes in a form.

I'm trying to write some code so that when a radio button is selected,
its corresponding checkbox is disabled.

My code looks like this:

function radioClicked(index)
{
document.table_config_form.my_checkbox[index].disabled = true;
You want to use
document.forms['table_config_form'].elements['my_checkbox'][index].disabled
= ...

instead, but much better it would be if you pass a reference to the element
collection to the method and not only the index of the element you want to
access. This allows the method for general use:

function disableElementOfGroup(oElementGroup, index)
{
if (typeof index == "undefined")
index = 0;

if (oElementGroup
&& oElementGroup[index]
&& typeof oElementGroup[index].disabled != "undefined")
oElementGroup[index].disabled = true;
}

<form name="table_config_form">
...
<input type="radio" name="foobar" value="1"
onclick="disableElementOfGroup(this.form.elements['myCheckBoxes'], 1)">
...
</form>
}

function radioChanged(index)
{
document.table_config_form.my_checkbox[index].disabled = false;
}
For the code of the two methods above is identical but the assigned value,
it is possible to add another argument and use ony one method instead:

function setGroupElDisabled(oElementGroup, index, bDisabled)
{
if (typeof index == "undefined")
index = 0;

if (oElementGroup
&& oElementGroup[index]
&& typeof oElementGroup[index].disabled != "undefined")
oElementGroup[index].disabled =
(typeof bDisabled != "undefined"
? bDisabled
: true);
}

<form name="table_config_form">
...
<input type="radio" name="foobar" value="1"
onclick="setGroupElDisabled(this.form.elements['myCheckBoxes'], 1,
true)">
...
</form>

You should also check if it is necessary that two or more checkboxes need
to have the same name, as with a unique name referencing is much easier:

function setDisabled(oElement, bDisabled)
{
if (oElement && typeof oElement.disabled != "undefined")
oElement.disabled =
(typeof bDisabled != "undefined"
? bDisabled
: true);
}

<form name="table_config_form">
...
<input type="radio" name="foobar" value="1"
onclick="setDisabled(this.form.elements['myCheckBox'], true)">
...
</form>
The radios' onClick calls radioClicked() with the appropriate index.
The radios' onChange calls radioChanged() with the appropriate index.
You need not to provide both event handlers, `onclick' is enough. But
you need to disable the first checkbox(es) in the `onclick' handler of
the second radio button and vice-versa.
It works fine in every respect except one: When the checkbox is
"disabled", it looks grayed out. However, when the user clicks on the
"disabled" checkbox, it behaves just like an enable checkbox!


It is a bug both in your code and in your UA.

Selecting the radiobutton, `onclick' fires and the checkbox is disabled.
Selecting the checkbox, the radio button loses focus (is changed), its
`onchange' handler fires and the checkbox is re-enabled again. (That's
the code issue, see above.)

Tested with IE 6 (SP1;Q330994;Q822925;Q828750) and Mozilla/5.0 (rv:1.5),
while Mozilla (in contrast to IE) correctly _not_ focuses the _disabled_
checkbox and so the `onchange' handler of the radio button doesn't fire.
(That's the UA issue.)
F'Up2 comp.lang.javascript

PointedEars

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cheddar | last post by:
I'm back again with another problem. What I want is for the user to click a checkbox and have a list appear. I think the code is almost there but it doesnt seem to be working, grrrr. Can anyone...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
1
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4...
4
by: RodBillett | last post by:
I have a situation where I have 3 checkboxes - and at least 1 needs to be selected at all times... I have implemented the code that allows this behavior to happen, BUT Viewstate gets all messed...
3
by: Marc Castrechini | last post by:
I have a page that changes an <ASP:Checkbox value based on a user entered value in a textboxm using client side Javascript. After my submit is fired the value for the chkMyCB.checked does not get...
5
by: Leo J. Hart IV | last post by:
Hello, I'm hoping someone can help me out. I was wondering if the Enabled property of a CheckBox or RadioButton server control is stored in the ViewState. If not, is there some way to to add...
4
by: Matrixreloadedth | last post by:
How to change disable color of Checkbox??? I have a checkbox with forecolor in red but when i disable by set Enable properties to false forecolor is changed to gray color but i don't want it. how...
8
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to...
3
by: AshishMishra16 | last post by:
Hi Friends, What is the way to iterate the Table and Make all the input types editable on the click of a checkbox. Here is what My Requirement is : <table id="adSearchTable"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.