CFAjax

 

Dynamically Loading Text

Example

This example dynamically loads state information based on the state selected.
Select a State :

How does this example works?

When you select a state from drop down list onChange event gets called onChange=loadInfo. The loadInfo function looks like this:
	function loadInfo()
	{
		var state = DWRUtil.getValue("state");
		DWREngine._execute(_cfscriptLocation, null, 'stateinfo', state, getResult);
	}
					
loadInfo function first gets the value of state selected , and then calls DWREngine._execute method which in turn calls stateinfo coldfusion function with the parameter as state code. The DWREngine parameters are explained below:
	_cfscriptLocation -> location of the coldfusion file that has the function implementation
	null, -> Default value
	'stateinfo', -> Coldfusion function to be called
	state -> State code for which we need the description
	getResult, -> Javascript function that will be called after stateinfo CF function has been executed
					
Coldfusion function stateinfo takes one argument state and return the information related to that state, listed below is the CF code:
		<cffunction name="stateinfo">		
			<cfargument name="state" required="yes" type="string">
			<cfset result = "">
			<cfif arguments.state eq "VA">
				<cfset result = result & "VA blah...">
			<cfelseif arguments.state eq "GA">
				<cfset result = result & "GA blah...">
			<cfelseif arguments.state eq "CA">
				<cfset result = result & "CA blah...">
			</cfif>
			<cfreturn result>
		</cffunction>
				
When the coldfusion method returns data, getResult JavaScript function gets executed with displays the text in the info span:
	function getResult(result)
	{
		document.getElementById("info").innerHTML = result;
	}