473,406 Members | 2,273 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,406 software developers and data experts.

Javascript with forms

Is there a way to produce 3 radio buttons, which when one is selected,
enables 11 check boxes which are otherwise disabled (greyed out)?

EXAMPLE:

Option 1 --Radio buttons
Option 2
Option 3

1 2 3 ... 11 --Check boxes, all disabled unless 'Option 3' is selected

I hope I'm making myself clear...

Marc
Oct 3 '05 #1
4 1783
Marc wrote:
Is there a way to produce 3 radio buttons, which when one is selected,
enables 11 check boxes which are otherwise disabled (greyed out)?

EXAMPLE:

Option 1 --Radio buttons
Option 2
Option 3

1 2 3 ... 11 --Check boxes, all disabled unless 'Option 3' is selected

I hope I'm making myself clear...


The following should do something like what you want. setTimeout is
used to run the function when the form is clicked so that if reset it
clicked, it has time to reset the form before runCheck is run.

If JavaScript is disabled, the checkboxes aren't disabled else users can
never select them. You may want to clear the checkboxes when disabling
them too - it's up to you.

<script type="text/javascript">

function doCheck(el, cbGroup)
{
var elChecked = el.checked;
if (cbGroup && cbGroup.length){
var i = cbGroup.length;
while (i--){
cbGroup[i].disabled = !elChecked;

// Uncomment the following if boxes are to
// be unchecked when disabled
// if (!el.checked) cbGroup[i].checked = false;

}
}
}

function runChecks()
{
if ( !document.getElementById
|| !document.getElementsByTagName ){
return;
}
var el = document.forms[0].elements['rg-1'][2];
var div = document.getElementById('checkGroup');
var cbGroup = div.getElementsByTagName('input');
doCheck(el, cbGroup);
}

window.onload = runChecks;

</script>
<!--
setTimeout used for onclick to allow reset to run before
runChecks runs
-->
<form action="" onclick="setTimeout('runChecks()',50);">
<div>
<input type="radio" name="rg-1" checked>Opt one<br>
<input type="radio" name="rg-1">Opt two<br>
<input type="radio" name="rg-1">Opt three<br>
</div>
<div id="checkGroup">
<input type="checkbox" name="cb-1">Check 1
<input type="checkbox" name="cb-2">Check 2
<input type="checkbox" name="cb-3">Check 3<br>
</div>
<div>
<input type="reset">
</div>
</form>


--
Rob
Oct 3 '05 #2
Marc wrote:
Is there a way to produce 3 radio buttons, which when one is selected,
enables 11 check boxes which are otherwise disabled (greyed out)?

EXAMPLE:

Option 1 --Radio buttons
Option 2
Option 3

1 2 3 ... 11 --Check boxes, all disabled unless 'Option 3' is selected

I hope I'm making myself clear...

Marc


Hi Marc,

Try something like this:
1) Name your radiobuttons (of course):eg myGroup
2) Add to each button a onClick eventhandler: checkIt()

You'll end up with something like this:

<form action="bla.php" name="orderform" Method="Post">
<input type="radion" name="myGroup" value="eggs" checked
onClick="checkIt('off');">eggs<br>
<input type="radion" name="myGroup" value="apples"
onClick="checkIt('off');">apples<br>
<input type="radion" name="myGroup" value="pears"
onClick="checkIt('on');">pears and subquestions active<br>

<br>
<input type="checkbox" name="bla1"> checkbox1<br>
<input type="checkbox" name="bla2"> checkbox2<br>
<input type="checkbox" name="bla3"> checkbox3<br>
</form>
In the above case the third option will enable the radiobuttons.

3) get the checked radiobutton
function checkIt(onoff){
var disabledcheckboxes = (onoff == 'off');
document.forms.orderForm.bla1.disabled = disabledcheckboxes;
document.forms.orderForm.bla2.disabled = disabledcheckboxes;
document.forms.orderForm.bla3.disabled = disabledcheckboxes;

}
}

Not tested, just some code to get you going.
In the above example I just send 'on' or 'off' to the function to decide
what to do.
If you want you can also loop over all radiobuttons and test for the one
that is checked, by checking the checked-property (will return true or
false).

Good luck,

Regards,
Erwin Moller
Oct 3 '05 #3
Thanks for your help. Please see my responses below.

RobG wrote:
The following should do something like what you want. setTimeout is
used to run the function when the form is clicked so that if reset it
clicked, it has time to reset the form before runCheck is run.
I don't need a reset button - this page is to be used as part of an
administration section, so presumably I can remove the setTimeout()
function?
If JavaScript is disabled, the checkboxes aren't disabled else users can
never select them. You may want to clear the checkboxes when disabling
them too - it's up to you.
I know JavaScript won't be disabled, as I know the environment the users
are running, do I need modify the script at all?
<script type="text/javascript">

function doCheck(el, cbGroup)
{
var elChecked = el.checked;
if (cbGroup && cbGroup.length){
var i = cbGroup.length;
while (i--){
cbGroup[i].disabled = !elChecked;

// Uncomment the following if boxes are to
// be unchecked when disabled
// if (!el.checked) cbGroup[i].checked = false;

}
}
}

function runChecks()
{
if ( !document.getElementById
|| !document.getElementsByTagName ){
return;
}
var el = document.forms[0].elements['rg-1'][2];
var div = document.getElementById('checkGroup');
var cbGroup = div.getElementsByTagName('input');
doCheck(el, cbGroup);
}

window.onload = runChecks;

</script>
<!--
setTimeout used for onclick to allow reset to run before
runChecks runs
-->
<form action="" onclick="setTimeout('runChecks()',50);">
<div>
<input type="radio" name="rg-1" checked>Opt one<br>
<input type="radio" name="rg-1">Opt two<br>
<input type="radio" name="rg-1">Opt three<br>
</div>
<div id="checkGroup">
<input type="checkbox" name="cb-1">Check 1
<input type="checkbox" name="cb-2">Check 2
<input type="checkbox" name="cb-3">Check 3<br>
</div>
<div>
<input type="reset">
</div>
</form>


I don't understand how the JavaScript detects which radio button is
selected, if all are called rg-1... Sorry, my knowledge of JS is
limited, would you mind explaining?

Marc
Oct 3 '05 #4
Marc wrote:
Thanks for your help. Please see my responses below.

RobG wrote:
The following should do something like what you want. setTimeout is
used to run the function when the form is clicked so that if reset it
clicked, it has time to reset the form before runCheck is run.

I don't need a reset button - this page is to be used as part of an
administration section, so presumably I can remove the setTimeout()
function?


Yes, but I think reset buttons are really handy...
If JavaScript is disabled, the checkboxes aren't disabled else users
can never select them. You may want to clear the checkboxes when
disabling them too - it's up to you.

I know JavaScript won't be disabled, as I know the environment the users
are running, do I need modify the script at all?


You could make all the checkboxes disabled by default and remove the
window.onload line. That would also let the reset button work properly
without the setTimeout (if you decide to keep it that is... :-) ).
<script type="text/javascript">

function doCheck(el, cbGroup)
{
var elChecked = el.checked;
if (cbGroup && cbGroup.length){
var i = cbGroup.length;
while (i--){
cbGroup[i].disabled = !elChecked;

// Uncomment the following if boxes are to
// be unchecked when disabled
// if (!el.checked) cbGroup[i].checked = false;

}
}
}

function runChecks()
{
if ( !document.getElementById
|| !document.getElementsByTagName ){
return;
}
var el = document.forms[0].elements['rg-1'][2];
This line gets a reference to the radio button to use for the check
- document.forms[0] is the form
- elements['rg-1'] is the collection of radio buttons
- [2] is the third button (the numbering starts from 0)
var div = document.getElementById('checkGroup');
This line gets a reference to the div containing the checkboxes
var cbGroup = div.getElementsByTagName('input');
And this one uses the div reference to get a reference to the collection
of input elements inside the div.
doCheck(el, cbGroup);
This line passes the element and collection references to doCheck
}
[...]


I don't understand how the JavaScript detects which radio button is
selected, if all are called rg-1... Sorry, my knowledge of JS is
limited, would you mind explaining?


Radio buttons in a group must all have the same name. You can access
them by their index within the radio group collection (see above).

By putting the onclick on the form, a click on any child element will
bubble up and run the form's onclick. It saves putting the onclick on
all three radio buttons.
--
Rob
Oct 3 '05 #5

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

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
3
by: bhanubalaji | last post by:
hi, I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA.. can any one help regarding this.. What's the wrong with my code? I am...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.