Quickie: Using JavaScript with a range prompt

You may occasionally need to reference the various prompts with JavaScript. IBM has a very nice document detailing how to access the various prompt components, unfortunately it seems to be missing a few options. Today I needed to access the “from” and “to” selects on a range prompt.

This works with a single select, range value prompt. I have not tested against other variations.

Assuming the prompt name is _test, the following will alert the selected value and caption:

<script>
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

fW._oLstChoicesrange_to_test.onchange = new Function ("alert(this.value);alert(fW._oLstChoicesrange_to_test[fW._oLstChoicesrange_to_test.selectedIndex].text)");
fW._oLstChoicesrange_from_test.onchange = new Function ("alert(this.value);alert(fW._oLstChoicesrange_from_test[fW._oLstChoicesrange_from_test.selectedIndex].text)");
</script>

This was tested on Cognos 10.1.1. I suspect it will work on 8.4 and 8.3.

report xml:

<report xmlns="http://developer.cognos.com/schemas/report/8.0/" useStyleVersion="10" expressionLocale="de">
				<modelPath>/content/folder[@name='Samples']/folder[@name='Models']/package[@name='Go Sales Paul Rewrite']/model[@name='model']</modelPath>
				<drillBehavior modelBasedDrillThru="true"/>
				<layouts>
					<layout>
						<reportPages>
							<page name="Page1">
								<style>
									<defaultStyles>
										<defaultStyle refStyle="pg"/>
									</defaultStyles>
								</style>
								<pageBody>
									<style>
										<defaultStyles>
											<defaultStyle refStyle="pb"/>
										</defaultStyles>
									</style>
									<contents><selectValue parameter="Parameter1" required="false" range="true" name="_test"><selectOptions><selectOption useValue="1"><displayValue>a</displayValue></selectOption><selectOption useValue="2"><displayValue>b</displayValue></selectOption><selectOption useValue="3"/><selectOption useValue="4"/><selectOption useValue="5"/></selectOptions></selectValue><HTMLItem>
			<dataSource>
				<staticValue>&lt;script&gt;
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

fW._oLstChoicesrange_to_test.onchange = new Function ("alert(this.value);alert(fW._oLstChoicesrange_to_test[fW._oLstChoicesrange_to_test.selectedIndex].text)");
fW._oLstChoicesrange_from_test.onchange = new Function ("alert(this.value);alert(fW._oLstChoicesrange_from_test[fW._oLstChoicesrange_from_test.selectedIndex].text)");
&lt;/script&gt;</staticValue>
			</dataSource>
		</HTMLItem></contents>
								</pageBody>
							</page>
						</reportPages>
					</layout>
				</layouts>
			<XMLAttributes><XMLAttribute name="RS_CreateExtendedDataItems" value="true" output="no"/><XMLAttribute name="listSeparator" value=";" output="no"/><XMLAttribute name="RS_modelModificationTime" value="2012-06-27T15:29:14.445Z" output="no"/></XMLAttributes></report>