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

Disabling fields based on a checkbox.

Hi,

There are lots of posts on this subject but after a couple of hours of
going though them I still can't get a number of fields to be disabled
when a checkbox is ticked. Basically I have a lot of Forecast and
Actual dates that can be entered, and next to each one an 'NA' checkbox
that I want to disable/grey-out both dates (and the JavaScript calendar
if possible). It also only has to work on Internet Explorer 6, as it
is for internal use only.

Any help greatly welcome as this is starting to drive me mad.

Code extract of form below.

Thanks,

Steve

<form name="lform" method="get" action="UpdateDB.asp">
[...]
<tr>
<td><b>Forecast1:</b> <input type="text" name="Forecast1" id="Date14"
size="10" maxlength="10" value="<%=ForeCast1%>">
<img src="/images/icons/calendar.gif" id="Trig14" style="cursor:
pointer; border: 1px solid red;" title="Date selector"></td>

<td><b>Actual1:</b> <input type="text" name="Actual1" id="Date15"
size="10" maxlength="10" value="<%=Actual1%>">
<img src="/images/icons/calendar.gif" id="Trig15" style="cursor:
pointer; border: 1px solid red;" title="Date selector">/</td>

<td><input type="checkbox" NAME="NA1" VALUE=1> N/A</td>
</tr>
[...]
</form>

Nov 9 '05 #1
6 3408
ML.Steve wrote:
There are lots of posts on this subject but after a couple of hours of
going though them I still can't get a number of fields to be disabled
when a checkbox is ticked.
What exactly have you not understood? What have you tried? What failed
with which error?
Basically I have a lot of Forecast and
Actual dates that can be entered, and next to each one an 'NA' checkbox
that I want to disable/grey-out both dates (and the JavaScript calendar
if possible).
Why, you just need to disable either both elements or their parent
`fieldset' element when the checkbox was clicked and is checked.
Any help greatly welcome as this is starting to drive me mad.
If I could *see* efforts on your part, I would gladly provide further help.
Code extract of form below.


There is not one line of JS/ECMAScript, so I have to doubt you have even
tried, let alone understood what you are doing.

<http://jibbering.com/faq/>
PointedEars
Nov 9 '05 #2
ML.Steve wrote:
Hi,

There are lots of posts on this subject but after a couple of hours of
going though them I still can't get a number of fields to be disabled
when a checkbox is ticked. Basically I have a lot of Forecast and
Actual dates that can be entered, and next to each one an 'NA' checkbox
that I want to disable/grey-out both dates (and the JavaScript calendar
if possible). It also only has to work on Internet Explorer 6, as it
is for internal use only.

Any help greatly welcome as this is starting to drive me mad.


Here's an example that should get you going:

<script type="text/javascript">

function disableControls(el)
{
var num = el.name.replace(/\D/g,'');
var form = el.form;
form['Forecast'+num].disabled = el.checked;
form['Actual'+num].disabled = el.checked;
}

</script>

<form name="lform" method="get" action="UpdateDB.asp">
<table border="1">
<tr>
<td><b>Forecast1:</b>
<input type="text" name="Forecast1"></td>
<td><b>Actual1:</b>
<input type="text" name="Actual1"></td>
<td><input type="checkbox" NAME="NA1"
onclick="disableControls(this);"> N/A</td>
</tr>
</table>
</form>
The trick is keep the number part of name of related controls the same.
If you wanted to get more complex, you could create an object that has
the name of the checkbox and the names of the controls to
enable/disable, like:
var relatedControls = {
'NA1' : ['Actual1','Forecast1'],
'NA2' : ['Actual2','Forecast2']
};
Then the function could do something like:

function toggleDisabled(el)
{
var form = el.form;
var controlNames = relatedControls[el.name];
var isChecked = el.checked;
var i = controlNames.length;
while (i--){
form[controlNames[i]].disabled = isChecked;
}
}

That way each checkbox could enable/disable any number of controls.

--
Rob
Nov 9 '05 #3
RobG wrote:
var num = el.name.replace(/\D/g,'');


More efficient is always:

var num = el.name.replace(/\D+/g, '');
PointedEars
Nov 9 '05 #4
Thanks for the replies;

The actual names of the fields are pretty long and detail what the date
field actually is, so looping is not an option. I have however got it
working from another post here, that I had tried before but didn't
work, but did a second time. The bit I am looking at now is disabling
the calendars, which I am completed stumped on. It would be great if I
could swap the calendar image for a disabled greyed out version which
didn't bring up the calendar.

<script language="JavaScript" type="text/JavaScript">
function enableDisable(CheckBoxID, FieldID1, FieldID2){
document.getElementById(FieldID1).disabled = (CheckBoxID.checked);
document.getElementById(FieldID2).disabled = (CheckBoxID.checked);
return true;
}

[...]

<td><b>Forecast:</b> <input type="text"
name="DrawingsApprovedforAcqAndPlanForecast" id="Date14" size="10"
maxlength="10" value="<%=DrawingsApprovedforAcqAndPlanForecast%>" >
<img src="/images/icons/calendar.gif" id="Trig14" style="cursor:
pointer; border: 1px solid red;" title="Date selector"></td>
<td><b>Actual:</b> <input type="text"
name="DrawingsApprovedforAcqAndPlan" id="Date15" size="10"
maxlength="10" value="<%=DrawingsApprovedforAcqAndPlan%>">
<img src="/images/icons/calendar.gif" id="Trig15" style="cursor:
pointer; border: 1px solid red;" title="Date selector"></td>
<td><input type="checkbox" NAME="DrawingsApprovedforAcqAndPlanNA" <% If
DrawingsApprovedforAcqAndPlanNA=1 Then%> CHECKED <% End If %>
onClick="return enableDisable(this, 'Date14', 'Date15');"
id="DrawingsApprovedforAcqAndPlanNA"> N/A</td>

[...]

<script type="text/javascript">

var NoOfCalendars=15;
for(var count=1; count<=NoOfCalendars; count=count+1 )
{
Calendar.setup(
{
inputField : "Date" + count,
button : "Trig" + count
}
)
}
</script>

Nov 9 '05 #5
Thomas 'PointedEars' Lahn wrote:
RobG wrote:

var num = el.name.replace(/\D/g,'');

More efficient is always:

var num = el.name.replace(/\D+/g, '');


or perhaps:

var num = el.name.replace(/^\w+/g,'');

I'd rather use hyphenated names and split so that the 'key' is explicit
and can contain any valid name characters (other than hyphen of course),
but there you go. :-)
--
Rob
Nov 9 '05 #6
RobG wrote:
Thomas 'PointedEars' Lahn wrote:
RobG wrote:
var num = el.name.replace(/\D/g,'');


More efficient is always:

var num = el.name.replace(/\D+/g, '');


or perhaps:

var num = el.name.replace(/^\w+/g,'');

I'd rather use hyphenated names and split so that the 'key'
is explicit [...]


With leading hyphen, I suppose, since /\w+/ does not match it.
PointedEars
Nov 9 '05 #7

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

Similar topics

0
by: LRW | last post by:
Subject line confusing? The whole thing is confusing to me. OK, I think the root of my problem, aside from mental, is array based...but I'm not even sure what I want to do is even possible. So if...
0
by: Ashish Shridharan | last post by:
Hi All, Has anyone ever tried disabling a checkbox or a radiobutton web server control in netscape ? While a textbox and a button control renders disabled, .NET adds the disabled attribute to...
4
by: omidmottaghi | last post by:
I need to disable/enable form elements in my form. the code i was developed works fine in FF, but in IE, its behaviour is very strange!! in the form, we have a lot of checkboxes, all of them...
1
by: Evelyne | last post by:
I create CheckBoxes on the fly. I want to disable the CheckBoxes when they reflect Status, so the operator does not try to input anything. However, the text/label of the CheckBox gets grayed out...
1
by: kebabkongen | last post by:
Hi, I'm working on a JavaScript that is enabling / disabling a select element according to whether a checkbox is selected or not. This works fine in Firefox, but in Internet Explorer (v 6.0.2900)...
1
by: Sofie | last post by:
hi, i have a form with a lot of fields on it. I would like to enable/disable all these fields using a checkbox. Is there a way to do this (perhaps using a loop mayb), without having to name each...
2
by: TGEAR | last post by:
there is one check box and if it is checked, then shows several fields underneath; otherwise, several fields are hidden. the default is unchecked. anyone can share the code for this function? ...
1
by: allamaria | last post by:
I am creating a form in Access, in the form there is a checkbox and some fields. I want to set the checkbox so, that if the user clicks on it, the fields should be enabled, if the checkbox has not...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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.