We help IT Professionals succeed at work.

Invalid Character

ael120898
ael120898 asked
on
I have the following function included among others in an html file:

.........
function setlist(f) {
 if (f.Category.selectedIndex==0) {f.Subcategory.options.length=0;return};
 secondlist=eval(f.Category.options[f.Category.selectedIndex].text);
 f.Subcategory.options.length=0;
 for(i=0;i<secondlist.length;i++) {
  f.Subcategory.options[i]=new Option(secondlist[i]);
 };
  f.Subcategory.selectedIndex=1;
};
.........

When running my file in IE5, i get the error that the line which contains the code "secondlist=eval(f.Category.options[f.Category.selectedIndex].text);" contains an invalid character. Which is the invalid character? The file works fine with IE5.5 and later. I cannot figure out the bug. Could you please tell me? Thank you.
Comment
Watch Question

Commented:
you cannot use .text. Yu should either use .name or .value

Regards,
CJ

Commented:
2CJ_S: The "option" object has a "text" property.
http://developer.netscape.com/docs/manuals/js/client/jsref/option.htm#1193380

2ael: Why you use "eval" here (assume the f variable represents a form object)?
Do you really need to evaluate the _text_ of selected option? If yes, what's the actual expression to evaluate?

Commented:
I see no need to use eval either.
But if you have to, remember that eval only take a string argument.

eval('f.Category.options[f.Category.selectedIndex].text');"

Author

Commented:
I am attaching you below the entire source code:

<HEAD><TITLE>Members Admin Page</TITLE>
<SCRIPT>

Activities=new Array()
 Activities[0]="                   ";
 Activities[1]="Puzzle ";
 Activities[2]="Mini Olympics ";
 Activities[3]="Tent Builup ";
 Activities[4]="Buid A Boat ";
 Activities[5]="Watersports ";
 Activities[6]="T-Shirt Competition ";
 Activities[7]="Aqua Treasure Hunt ";
 Activities[8]="Jeep Safari ";
 Activities[9]="Flying ";
 Activities[10]="Boat Mediterraneo ";
 Activities[11]="Boat Jolly Rodgers ";
 Activities[12]="Paphos Aquarium ";

Boat_Trips=new Array()
 Boat_Trips[1]="        ";
 
 
 Hotels=new Array()
 Hotels[0]="                   ";
 Hotels[1]="Limassol Le Meridien ";
 Hotels[2]="Limassol Mediterranean ";
 Hotels[3]="Limassol Amathus ";
 Hotels[4]="Limassol Four Seasons ";
 Hotels[5]="Paphos ";
 Hotels[6]="Ayia Napa ";
 Hotels[7]="Nicosia ";
 Hotels[8]="Larnaca ";
 
 Team_Building_Events=new Array()
 Team_Building_Events[1]="        ";

Theme_Nights=new Array()
 Theme_Nights[0]="            ";
 Theme_Nights[1]="Omodos Fiesta ";
 Theme_Nights[2]="Crusader ";
 
 
 Coaches=new Array()
 Coaches[0]="            ";
 Coaches[1]="Panayides ";
 Coaches[2]="Antonis ";
 
 
 Restaurants=new Array()
 Restaurants[0]="                 ";
 Restaurants[1]="Limassol - Istorikon ";
 Restaurants[2]="Limassol - Stamna ";
 Restaurants[3]="Limassol - Sands ";
 Restaurants[4]="Limassol - La Maison Fleurie ";
 Restaurants[5]="Limassol - Alourgida ";
 Restaurants[6]="Limassol - Skaros ";
 Restaurants[7]="Paphos ";
 Restaurants[8]="Ayia Napa ";
 Restaurants[9]="Nicosia ";
 Restaurants[10]="Larnaca ";


Group_Photos=new Array()
 Group_Photos[1]="        ";


AV_Equipment=new Array()
 AV_Equipment[1]="        ";

 
function setlist(f) {
 if (f.Category.selectedIndex==0) {f.Subcategory.options.length=0;return};
 secondlist=eval(f.Category.options[f.Category.selectedIndex].text);
 f.Subcategory.options.length=0;
 for(i=0;i<secondlist.length;i++) {
  f.Subcategory.options[i]=new Option(secondlist[i]);
 };
  f.Subcategory.selectedIndex=1;
};
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<H1 align=center> Members Administration Page </H1><font face="Arial" size="2"><font color=red>*
indicates required fields</font><BR>
</font>
<form name="form1" action="http://www.palmdmc.com.cy/cgi-bin/webdata_palmphoto.pl" method=post ENCTYPE="multipart/form-data">
<input type=hidden name="ID">
<TABLE BORDER=0>
<TR><TD><font color=orange face="Arial" size="2">Date Created</font></TD><TD><font face="Arial" size="2"><input type=text size=15 name="date_created">
    (Date is added automatically, use for Search/modify only)</font></TD></TR>
<TR><TD><font face="Arial" size="2"><font color=red>* </font>Category</font></TD><TD><font face="Arial" size="2">
<SELECT NAME="Category" onChange="setlist(form)">
<OPTION VALUE="">Show all Categories.&nbsp
<OPTION>Activities&nbsp
<OPTION>Boat_Trips&nbsp
<OPTION>Hotels&nbsp
<OPTION>Team_Building_Events&nbsp
<OPTION>Theme_Nights&nbsp
<OPTION>Coaches&nbsp
<OPTION>Restaurants&nbsp
<OPTION>Group_Photos&nbsp
<OPTION>AV_Equipment&nbsp
</SELECT>
</font></TD></TR>
<TR><TD><font face="Arial" size="2"><font color=red>* </font>Subcategory</font></TD><TD><font face="Arial" size="2">
<SELECT NAME="Subcategory">
</font></TD></TR>
<TR><TD><font face="Arial" size="2"><font color=red>* </font>Thumbnail</font></TD><TD><font face="Arial" size="2"><INPUT NAME="Thumbnail" TYPE="file"></font></TD></TR>
<TR><TD><font face="Arial" size="2"><font color=red>* </font>Link</font></TD><TD><font face="Arial" size="2"><input type=text name="Link" size=10></font></TD></TR>
<TR><TD><font face="Arial" size="2">Description</font></TD><TD><font face="Arial" size="2"><input type=text name="Description" size=20></font></TD></TR>

<!-- Do not change the values of the submit buttons -->
</TABLE><font face="Arial" size="2"><INPUT TYPE=SUBMIT NAME="cgifunction" VALUE="add"> <INPUT TYPE=SUBMIT NAME="cgifunction" VALUE="Search/modify"> <INPUT TYPE=RESET VALUE="Clear Form"> <INPUT TYPE=SUBMIT NAME="cgifunction1" VALUE="Search"></font></FORM>

<font face="Arial" size="2">

&nbsp;</font>
<p>
  <A HREF="http://www.palmdmc.com.cy/library">
    <font face="Arial" size="2">
    Return to home page</font></A>      
  <font face="Arial" size="2">      
<BR><A HREF="javascript:logout()">Log out</A>
  </font>
 </p>

However, if i remove "text" and use either "value" or "name" nothing works. Instead i get an error saying that secondlist.length is null or not an object.
What should i use then?
Commented:
Remove the "&nbsp;" from all option's texts in "category" select. Because of these texts is used as names of arrays.
And add a closing </select> tag for subcategory select. :o)

Author

Commented:
Thank you so much for your help! My mistake was so obvious but i just couldn't realise it. I really appreciate your help. Thank you! You deserve all points!

Commented:
Thank you for awarding me points :) Good luck!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.