I'm working on creating the public side of a plugin and am getting a javascript error that does not occur when I run the same code outside of Mura or when I run it on jsfiddle.
The javascript is pretty straight forward. It populates a form select based on the value of a different select. Here is a jsfiddle of the html and javascript which works by itself. http://jsfiddle.net/P7ZTz/2/
In my public view of the plugin I have the following.
<cfset rc.$.loadJSLib() />
<cfset rc.pc.addToHTMLHeadQueue("addendaJsInclude.cfm")>
<form name="form" method="post" action="addenda2.cfm" onSubmit="return checkrequired(this)">
<select name="mRoute" onchange="javascript:populateData(this.options[selectedIndex].text)">
<option value="">Select Route</option>
<option value="Route 3">Route 3</option>
<option value="Route 2">Route 2</option>
<option value="Route 1">Route 1</option>
</select>
<select name="mSection">
<option value="">Section</option>
</select>
<input type="submit" name="submit">
</form>
My addendaJsInclude.cfm file includes the following:
var arrayData = new Array();
arrayData[0] = 'Route 1|1|'
arrayData[1] = 'Route 1|2|'
arrayData[2] = 'Route 1|3|'
arrayData[3] = 'Route 1|4|'
arrayData[4] = 'Route 1|5|'
arrayData[5] = 'Route 1|6|'
arrayData[6] = 'Route 1|7|'
arrayData[7] = 'Route 2|1|'
arrayData[8] = 'Route 3|1|'
function populateData( name ) {
select = window.document.form.mSection;
string = "";
count = 1;
select.options.length = count;
for( i = 0; i < arrayData.length; i++ ) {
string = arrayData[i].split( "|" );
if( string[0] == name ) {
select.options[count++] = new Option( string[1] );
}
}
}
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,1)=="m") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(1,12).toUpperCase();
alert("Please select a "+shortFieldName+".");
return false;
}
else
return true;
}
When I execute the page in my browser I get the following error in my console:
select is undefined
select.options.length = count;
This seems to be some type of scoping issue, but I'm too much of a javascript novice to know what the issue is.Again, it does run fine on jsfiddle and in a regular html page outside of Mura. As it runs on the client side I'm a little lost why it would behave differently when inside a Mura output. The html and javascript looks the same for both, but the error only occurs in the Mura page. Any input would be greatly appreciated.