473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select box options to disable/enable checkbox?

I'm a beginner to javascript and need help. I've searched the forum but
can't piece the answer together.

I need to be able to uncheck and disable a checkbox based on the option
selected in a Select list.

This is what I tried. Any help is appreciated.
==============
<script type="text/javascript">
function GetOptVal(OptVa l)
var OptVal;
{
if(OptVal="XX") document.form1. chkbox1.disable d = false;
}
else
{
document.form1. chkbox1.checked = false;
document.form1. chkbox1.disable d = true;
}
</script>

<form name="form1">

<select name="Opt1"
onchange=GetOpt Val(document.fo rm1.Opt1[document.form1. Opt1.selectedIn dex].value)>
<option value=" ">Select One</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
<option value="ZZ">ZZ</option>
</select>

<input type="checkbox" name="chkbox1" />
</form>

Jan 18 '06 #1
2 12827
bigrich wrote:
<script type="text/javascript">
function GetOptVal(OptVa l)
var OptVal;
{ ^
Fantasy syntax. It has to be

function /identifier/(/comma-separated list of named arguments/)
{

where the /.../ parts are to be replaced with meaningful code.

Local variables are created automatically for named arguments;
redeclaring them with a `var' statement is definitely harmful.
if(OptVal="XX") document.form1. chkbox1.disable d = false; ^[1] ^^^^^^^^^^^^^^^ ^^^^^^^[2]
[1] The `=' operator specifies an assignment always.
You are looking for `==' (type-converting equals)
or `===' (strict equals).
} ^
Nesting error. If you would have indented the blocks, being a sign
of good code style in any structured programming language, you would
have noticed.
else
{
document.form1. chkbox1.checked = false; ^^^^^^^^^^^^^^^ ^^^^^^^[2] document.form1. chkbox1.disable d = true; ^^^^^^^^^^^^^^^ ^^^^^^^[2]

[2] Avoid obtaining the reference to the same object repeatedly.
It is inefficient regarding runtime and maintenance.
} </script>

<form name="form1">

<select name="Opt1"
onchange=GetOpt Val(document.fo rm1.Opt1 ^ [1]^^^^^^^^^^^^^^^ ^^^^^[1]

[1] Attribute values that contain certain characters, such as "(",
must be delimited with in single (') or double (") quotes.
Therefore you should quote attribute values always.

[2] The element object that fired the event can be referred with
`this' in event handler attribute values.
[document.form1. Opt1.selectedIn dex].value)> ^ <option value=" ">Select One</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
<option value="ZZ">ZZ</option>
</select>

<input type="checkbox" name="chkbox1" /> ^
This is X(HT)ML syntax, however you write HTML (as can be inferred
from your `script' element's content). So omit the forward slash.
</form>


Probably you are looking for something like this:

<script type="text/javascript">
function enableDisable(o , sName, b)
{
var o = o.form.elements[sName];
if (o)
{
// the specified control will enabled and checked,
// or disabled and unchecked
o.disabled = !b;
o.checked = b;
}
}
</script>

<form action="" ...>
<select name="Opt1"
onchange=
"enableDisable( this, 'chkbox1', this[this.selectedIn dex].value == 'XX');">
<option value=" * *">Select One</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
<option value="ZZ">ZZ</option>
</select>
<input type="checkbox" name="chkbox1">
</form>

Please adhere to the FAQ <URL:http://jibbering.com/faq/>,
especially <URL:http://jibbering.com/faq/#FAQ4_43>, next time.
PointedEars
Jan 18 '06 #2

"bigrich" <bi*****@gmail. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
I'm a beginner to javascript and need help. I've searched the forum but
can't piece the answer together.

I need to be able to uncheck and disable a checkbox based on the option
selected in a Select list.

This is what I tried. Any help is appreciated.
==============
<script type="text/javascript">
function GetOptVal(OptVa l)
var OptVal;
{
if(OptVal="XX") document.form1. chkbox1.disable d = false;
}
else
{
document.form1. chkbox1.checked = false;
document.form1. chkbox1.disable d = true;
}
</script>

<form name="form1">

<select name="Opt1"
onchange=GetOpt Val(document.fo rm1.Opt1[document.form1. Opt1.selectedIn dex].value)>
<option value=" ">Select One</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
<option value="ZZ">ZZ</option>
</select>

<input type="checkbox" name="chkbox1" />
</form>

Several errors here:

1. 1st { should come after "function GetOptVal(OptVa l)"
2. onchange needs quotes
3. if(OptVal="XX") should be using == instead of =

I would change it like so:

<script type="text/javascript">
var OptVal;
function GetOptVal(OptVa l) {
chkbox = document.form1. chkbox1;
if(OptVal=="XX" ) chkbox = false;
else {
chkbox1.checked = false;
chkbox1.disable d = true;
}
}
</script>

<form name="form1">

<select name="Opt1"
onchange="GetOp tVal(document.f orm1.Opt1[document.form1. Opt1.selectedIn dex].value);">
<option value=" ">Select One</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
<option value="ZZ">ZZ</option>
</select>

<input type="checkbox" name="chkbox1" />
</form>

Jan 18 '06 #3

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

Similar topics

2
9745
by: HolaGoogle | last post by:
Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have to field.One is a "yes/no" field and another one is "number" field. To display the yes/no field in my asp form i use a checkbox and fot he other field i use a normal text box. if the yes/no field is checked then the other field is enabled otherwise it has to be disabled.Here's what i've done so far: Do While Not ObjRS.EOF <td><input...
2
6470
by: HolaGoogle | last post by:
Can you please tell me the right way to do this?? it's realy important! thanks in advance... Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have to field.One is a "yes/no" field and another one is "number" field. To display the yes/no field in my asp form i use a checkbox and fot he other field i use a normal text box. if the yes/no field is checked then the other field is enabled...
12
8884
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
3
1672
by: Toboja | last post by:
Hello, I have two select lists: Available Sort Options and Selected Sort Options. The user clicks on any of the available sort options and can move them to the selected sort options box. I use the typical MoveOption javascript functions to achieve this. I need to enable the user to also decide whether they want to also subtotal using any of the options in the selected sort box. I thought that it would be nice to be able to add a checkbox...
4
10426
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 to make disable color as the same before set enable = false Anyone can help me?????
1
2810
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) it appears wierd: When I disable the selevt element in IE, it continues to appear as enabled (falsely) until I try changing it. When I click on it, updates itself as grey as to indicate that it is disabled.
2
26371
by: Kevin | last post by:
I've been looking all over and I can't seem to find what ought to be simple. I need to disable a drop down when a checkbox is checked, and enable it when same checkbox is unchecked. I've found any number of scripts to disable / enable a drop down but they are all made to work with 2 different buttons, radio group selections, etc. I can't seem to come up with one that can toggle off a single checkbox.
4
3394
by: joseff | last post by:
Hi evryone!! I' m very new not only to Python and Tkinter but to programing. I'm a bit stuck here and would be grateful for some help. I have 3 radio select buttons(see below) which I want to link to a pmw dialog with (apply and cancel options ) i.e. when I press enable btn dialog pops up and asks to confirm the my choice and if apply is selected then run my enable script if cancel then close the dialog. the same with the other radio...
3
1906
by: yurcheg | last post by:
Hi, I've searched the forum for this problem but haven't found anything relevant. I've got a javascript code which dynamically creates list-boxes in a form. It works perfectly fine in IE, but in Firefox 2 I change the selected value in a list-box but nothing gets submitted via POST. The dynamic list-box creation code looks like this: <form name="form0" method=POST action="/dynamic/AppDirector/Global/GlobalParameters" onkeypress="return...
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.