Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

javascript error

Hi,

I am getting this error
test1.aspx:11 Uncaught TypeError: Cannot read property 'elements' of undefined
InitializeArrays
@ test1.aspx:11(anonymous function) @ test1.aspx:6

The code works in IE but in Chrome, any ideas?  thanks

        
<SCRIPT>    var row = 500;
    var countElements = 0;
    var totalLists = 10;
    var indexArray = new Array((totalLists + 1));
    var lArray = new Array(row);
    InitializeArrays();
    //if ('<%=request.QueryString("save")%>' == 1)
      //  AddSavedElements(document.formTest);
    function InitializeArrays() {
        j = 0;
        for (var i = 0; i < document.formTest.elements.length; i++) {
            if (document.formTest.elements(i).type == "select-multiple") {
                lname = document.formTest.elements(i).name;
                listObj = FindList(document.formTest, lname);
                indexArray[j] = new Array();
                indexArray[j][0] = listObj.length;
                indexArray[j][1] = lname;
                j++;
            }
        }
        for (var i = 0; i < row; i++)
            lArray[i] = new Array();
    }

    function AddElement(frm) {
        if (!isSelected(frm, "l"))
            alert("You must select an item from at least one pannel on the left side.");
        else {
            var eArray = new Array();
            var tArray = new Array();
            //looks for all the selects on the left side that contains selected items
            for (var i = frm.elements.length - 1; i >= 0; i--) {
                if (frm.elements(i).type == "select-multiple" && frm.elements(i).name != "mainlist") {
                    //once the select is found then it will go through all the selected items
                    //within the select and delete them from bottom to top
                    for (var j = frm.elements(i).length - 1; j >= 0; j--) {
                        if (frm.elements(i).options[j].selected) {
                            value = frm.elements(i).options[j].value;
                            text = frm.elements(i).options[j].text;
                            eArray[countElements] = value;
                            tArray[countElements] = text;
                            if (!isInArray(value, lArray)) {
                                lArray[countElements][0] = frm.elements(i).options[j].value;
                                lArray[countElements][1] = frm.elements(i).name;
                                lArray[countElements][2] = 1;
                                lArray[countElements][3] = frm.elements(i).options[j].text;
                            }
                            RemoveOption(frm.elements(i), j);
                            DecrementIndexes(frm.elements(i).name);
                            countElements++;
                        }
                    }
                }
            }
            for (var j = eArray.length - 1; j >= 0; j--) {
                if (eArray[j] != null) {
                    AddOption(frm.mainlist, eArray[j], tArray[j]);
                    eArray[j] = null;
                    tArray[j] = null;
                    IncrementIndexes("mainlist");
                }
            }
        }
    }
    function AddListElements(frm, list) {
        if (list.length != 0) {
            var eArray = new Array();
            var tArray = new Array();
            //looks for all the selects on the left side that contains selected items
            for (var j = list.length - 1; j >= 0; j--) {
                value = list.options[j].value;
                text = list.options[j].text;
                eArray[countElements] = value;
                tArray[countElements] = text;
                if (!isInArray(value, lArray)) {
                    lArray[countElements][0] = list.options[j].value;
                    lArray[countElements][1] = list.name;
                    lArray[countElements][2] = 1;
                    lArray[countElements][3] = list.options[j].text;
                }
                RemoveOption(list, j);
                DecrementIndexes(list.name);
                countElements++;
            }
            for (var j = eArray.length - 1; j >= 0; j--) {
                if (eArray[j] != null) {
                    AddOption(frm.mainlist, eArray[j], tArray[j]);
                    eArray[j] = null;
                    tArray[j] = null;
                    IncrementIndexes("mainlist");
                }
            }
        }
    }
    function AddAllElements(frm) {
        for (var i = 0; i < frm.elements.length; i++) {
            if (frm.elements(i).type == "select-multiple" && frm.elements(i).name != "mainlist") {
                AddListElements(frm, frm.elements(i));
            }
        }
    }
    function RemoveElement(frm) {
        if (!isSelected(frm, "r"))
            alert("You must select at least one item from the right side.");
        else {
            for (var j = frm.mainlist.length - 1; j >= 0; j--) {
                if (frm.mainlist.options[j].selected) {
                    setArray(frm.mainlist.options[j].value);
                    RemoveOption(frm.mainlist, j);
                    DecrementIndexes("mainlist");
                }
            }
            for (var j = lArray.length - 1; j >= 0; j--) {
                if (lArray[j][2] == 2) {
                    indexList = FindElement(lArray[j][0]);
                    list = FindList(frm, lArray[indexList][1]);
                    if (!optionExists(list, lArray[j][0])) {
                        AddOption(list, lArray[indexList][0], lArray[indexList][3]);
                        IncrementIndexes(list.name);
                    }
                    lArray[j][2] = 1;
                }
            }
        }
    }
    function RemoveListElements(frm, list) {
        if (frm.mainlist.length != 0) {
            for (var j = frm.mainlist.length - 1; j >= 0; j--) {
                if (inList(frm.mainlist.options[j].value, list)) {
                    setArray(frm.mainlist.options[j].value);
                    RemoveOption(frm.mainlist, j);
                    DecrementIndexes("mainlist");
                }
            }
            for (var j = lArray.length - 1; j >= 0; j--) {
                if (lArray[j][2] == 2) {
                    indexList = FindElement(lArray[j][0]);
                    list = FindList(frm, lArray[indexList][1]);
                    if (!optionExists(list, lArray[j][0])) {
                        AddOption(list, lArray[indexList][0], lArray[indexList][3]);
                        IncrementIndexes(list.name);
                    }
                    lArray[j][2] = 1;
                }
            }
        }
    }
    function RemoveAllElements(frm) {
        for (var i = 0; i < frm.elements.length; i++) {
            if (frm.elements(i).type == "select-multiple" && frm.elements(i).name != "mainlist") {
                RemoveListElements(frm, frm.elements(i));
            }
        }
    }
    function MoveUp(frm) {
        for (var i = 0; i < frm.mainlist.length; i++) {
            if (frm.mainlist.options[i].selected) {
                SwapUp(frm, i);
            }
        }
    }
    function SwapUp(frm, sIndex) {
        i = sIndex;
        if (i > 0) {
            value1 = frm.mainlist.options[i].value;
            value2 = frm.mainlist.options[i - 1].value;
            text1 = frm.mainlist.options[i].text;
            text2 = frm.mainlist.options[i - 1].text;
            option = new Option(text2, value2);
            frm.mainlist.options[i] = option;
            option = new Option(text1, value1);
            frm.mainlist.options[i - 1] = option;
            frm.mainlist.options[i - 1].selected = true;
        }
        if (i == 1)
            frm.mainlist.options[i - 1].selected = false;
    }
    function MoveDown(frm) {
        for (var i = frm.mainlist.length - 1; i >= 0; i--) {
            if (frm.mainlist.options[i].selected) {
                SwapDown(frm, i);
            }
        }
    }
    function SwapDown(frm, sIndex) {
        i = sIndex;
        if (i < frm.mainlist.length - 1) {
            value1 = frm.mainlist.options[i].value;
            value2 = frm.mainlist.options[i + 1].value;
            text1 = frm.mainlist.options[i].text;
            text2 = frm.mainlist.options[i + 1].text;
            option = new Option(text2, value2);
            frm.mainlist.options[i] = option;
            option = new Option(text1, value1);
            frm.mainlist.options[i + 1] = option;
            frm.mainlist.options[i + 1].selected = true;
        }
        if (i == frm.mainlist.length - 2)
            frm.mainlist.options[i + 1].selected = false;
    }
    function isInArray(value, Array) {
        var inArray = false;
        for (var i = 0; i < Array.length && !inArray; i++) {
            if (Array[i][0] == value)
                inArray = true;
        }
        return inArray;
    }
    function inList(value, list) {
        found = false;
        for (var i = 0; i < lArray.length && !found; i++) {
            if (lArray[i][0] == value && lArray[i][1] == list.name)
                found = true;
        }
        return found;
    }
    function optionExists(list, value) {
        var found = false;
        for (var i = 0; i < list.length; i++)
            if (list.options[i].value == value)
            found = true;
        return found;
    }
    function setArray(value) {
        found = false;
        for (var i = 0; i < lArray.length; i++) {
            if (lArray[i][0] == value) {
                lArray[i][2] = 2;
                found = true;
            }
        }
    }
    function AddOption(list, value, text) {
        //Option("TheText","TheValue")
        option = new Option(text, value);
        var index = getIndex(list);
        list.options[index] = option;
    }
    function RemoveOption(list, index) {
        list.options[index] = null;
    }
    //Returns an object that references to the select element within the form
    function FindList(frm, name) {
        var found = false;
        var listObject = null;
        for (var i = 0; i < frm.elements.length && !found; i++) {
            if (frm.elements(i).name == name) {
                listObject = frm.elements(i);
                found = true;
            }
        }
        return listObject;
    }
    //Returns the index of the element moved to the right pannel
    function FindElement(key) {
        var found = false;
        var j = 0;
        for (var i = countElements - 1; i >= 0 && !found; i--)
            if (lArray[i][0] == key) {
            j = i;
            found = true;
        }
        return j;
    }
    function isSelected(frm, opt) {
        var selected = false;
        if (opt == "l") {

            for (var i = 0; i < frm.elements.length && !selected; i++) {

                if (frm.elements(i).type == "select-multiple" && frm.elements(i).name != "mainlist") {
                    if (frm.elements(i).value != "")
                        selected = true;

                }

            }
        }
        else {

            if (frm.mainlist.value != "") {
                selected = true;
            }
        }
        return selected;
    }
    function IncrementIndexes(lname) {
        found = false;
        for (var i = 0; i < indexArray.length && !found; i++)
            if (indexArray[i][1] == lname) {
            indexArray[i][0]++;
            found = true;
        }
    }
    function DecrementIndexes(lname) {
        found = false;
        for (var i = 0; i < indexArray.length && !found; i++)
            if (indexArray[i][1] == lname) {
            indexArray[i][0]--;
            found = true;
        }
    }
    function getIndex(lname) {
        found = false;
        var index = 0;
        for (var i = 0; i < indexArray.length && !found; i++) {
            if (indexArray[i][1] == lname.name) {
                index = indexArray[i][0];
                found = true;
            }
        }
        return index;
    }
    function DisplayArray() {
        empty = true;
        for (var i = 0; i < lArray.length && lArray[i][0] != undefined; i++) {
            document.write(i + " " + lArray[i][0] + " " + lArray[i][1] + " " + lArray[i][2] + " " + lArray[i][3] + "<br>");
            empty = false;
        }
        if (empty)
            alert("Array is empty");
    }
    function SendValues(frm) {
        if (frm.mainlist.length == 0)
            alert("There is no fields to be displayed");
        else {
            listvalue = new String();
            listvalue = "";
            listdesc = new String();
            listdesc = "";
            listcat = new String();
            listcat = "";
            for (var i = 0; i < frm.mainlist.length; i++) {
                listval = frm.mainlist.options[i].value;
                value = listval.substring(0, listval.indexOf(":"));
                cat = listval.substring(listval.indexOf(":") + 1, listval.length);
                listvalue = listvalue + value + ",";
                listcat = listcat + cat + ",";
                listdesc = listdesc + frm.mainlist.options[i].text + ",";
            }
            listvalue = listvalue.substring(0, listvalue.length - 1);
            frm.fieldlistvalue.value = listvalue;
            listcat = listcat.substring(0, listcat.length - 1);
            frm.fieldlistcat.value = listcat;
            listdesc = listdesc.substring(0, listdesc.length - 1);
            frm.fieldlistdesc.value = listdesc;
            frm.submit();
        }
    }
    function LoadReport(frm) {
        if (frm.SavedReports.value != "") {
            frm.action = "adhocReportCol.asp?save=1";
            frm.SavedReportID.value = frm.SavedReports.value;
            frm.submit();
        }
        else {
            frm.action = "adhocReportCol.asp?ScrSt=1";
            frm.submit();
        }
    }
</SCRIPT>

       





<form name="formTEST" method="post">


	

<TABLE border = "1" width="910"  class="bankth">
	    <tr>

		    <td colspan=2>&nbsp;</td>
		    
		    <td colspan=2>&nbsp;</td>
	    </tr> 
		<TR>

			<TD width=250>

			<SELECT multiple size="7" name="testCategory" CLASS="AdHoc_Form_LEFT"> 
			      <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
			</SELECT> 
			</TD>
			<TD width=30>
			    <INPUT onclick=AddElement(this.form); type=button value=" > "><BR>
			</TD>
		
			<TD width=125 align=center>
				Move All Categories
				<INPUT style="WIDTH: 30px" onclick=AddAllElements(this.form); type=button value=">>"><BR>
				<INPUT style="WIDTH: 30px" onclick=RemoveAllElements(this.form); type=button value="<<"><BR>
			</TD>
			<TD  width=250 valign=bottom>
				<SELECT  CLASS="AdHoc_Form_RIGHT" multiple size=17 name=mainlist></SELECT> 
			</TD>
			<TD  width=240>
			    Change Columns Order<br />
				<INPUT onclick=MoveUp(this.form) type=button value="    Move Up   "><br>
				<INPUT onclick=MoveDown(this.form) type=button value=" Move Down "> 
			</TD>     		
			
		</TR>

</TABLE>
</form>
            

Open in new window

Avatar of Kim Walker
Kim Walker
Flag of United States of America image

On lines 13 and 14, it should have square brackets instead of parentheses.
            if (document.formTest.elements[i].type == "select-multiple") {
                lname = document.formTest.elements[i].name;

Open in new window

Edited to include line 14
SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg
mcrmg

ASKER

yes, I have replaced all element(i) to element (square) i (square), now I am getting this error

Uncaught TypeError: Cannot read property 'elements' of undefined


What I dont understand is why the code works in IE..thanks
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

working now..thank you
You are welcome.
You are welcome.

Regarding IE. IE has always been in a separate world from other browsers. They got in on the ground floor before there were standards to follow and were reluctant to adopt those standards when they were released. They have grandfathered in a great many of their unique methods so that pages created specifically for IE would continue to work even after they began to migrate toward compliance with standards. I believe using parentheses for array index designations was one of those. Even the latest versions of IE and Edge are still somewhat lacking in standards compliance though they continue to improve. Never rely on IE or Edge to confirm that your HTML, CSS, or Javascript works.
Avatar of mcrmg

ASKER

Thank you very much for the followed up answer.