CFLib.org – Common Function Library Project

ToggleValue(variable, value1, value2)

Last updated July 3, 2002

Version: 1 | Requires: ColdFusion 5 | Library: UtilityLib

 
Rated 1 time(s). Average Rating: 4.0

Description:
Save yourself a couple lines of coding next time you need to toggle the value of a variable.

Return Values:
Returns a string.

Example:

view plain print about
<cfset action = "On">
<cfset newValue = toggleValue(action,"On","Off")>
<cfoutput>Action = #action#, toggle is #toggleValue(action,"On","Off")#</cfoutput>

Parameters:

Name Description Required
variable The variable that stores the value you will toggle. Yes
value1 The first value of the toggle. Yes
value2 The second value of the toggle. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Toggles a value (ie: &quot;stop&quot;/&quot;start&quot;) between two options.
 * 
 * @param variable      The variable that stores the value you will toggle. (Required)
 * @param value1      The first value of the toggle. (Required)
 * @param value2      The second value of the toggle. (Required)
 * @return Returns a string. 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, July 3, 2002 
 */

function toggleValue(variable,value1,value2){
    //make a struct in which the value is the opposite of the key
    var toggler = structNew();
    toggler[value1] = value2;
    toggler[value2] = value1;
    //return whichever value is not the current value
    return toggler[variable];
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson