Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable checkbox depending on dropdown selection? 1

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I have a checkbox 'roundtrip' that when the page is first loaded, it is enabled. I have a dropdown, 'to_site', that populates with several locations including 'HOME' and 'OTHER'.

What I want to happen is that when a user selects either 'HOME' or 'OTHER' from the dropdown, the checkbox is disabled. Additionally, just to cover all possibilities, if they select 'HOME' or 'OTHER' then change their mind, it enables the checkbox.

Any assistance would be great.
 
something like:

Code:
function doThing(sel,cb) {
    var aryVals = new Array('HOME', 'OTHER');
    var makeDisabled = false;
    for ( var i = 0; i < aryVals.length; i++ ) {
        if ( sel.options[sel.selectedIndex].value == aryVals[i] )
            makeDisabled = true;
    }
    cb.disabled = makeDisabled;
}

call it like:

Code:
<select onchange="doThing(this, this.form.cbName );">

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

[banghead]
 
Works as expected, thanks a lot!!!

One twist that I failed to mention...

The form actually has two dropdowns, a "To Location" and a "From Location". A user can pick 'HOME' from the "From Location" and the checkbox is disabled. They can then select 'OTHER' from the "To Location", the checkbox is still disabled. So far so good!

However, if before submit, then go back and change the "From Location" to something other than 'HOME' or 'OTHER' with the "To Location" still set to 'OTHER', the function fires and enables the checkbox.

I realize why it is doing what it is doing but still looking at tweaking it just a bit to catch every scenario the user could throw at it. Any thoughts on modifying the function to pass in the value of both dropdowns and checking them?

Thanks again.
 
Got it!

Function:

function loc_check(selTo,selFrom,cb) {
var aryVals = new Array('HOME', 'OTHER');
var makeDisabled = false;
for ( var i = 0; i < aryVals.length; i++ ) {
if ( selTo.options[selTo.selectedIndex].value == aryVals )
makeDisabled = true;
if ( selFrom.options[selFrom.selectedIndex].value == aryVals )
makeDisabled = true;
}
cb.disabled = makeDisabled;
}

Function Call:
onchange='loc_check(to_site, from_site, document.form1.roundtrip)'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top