/    Sign up×
Community /Pin to ProfileBookmark

Javascript function help

Below is an html extract and a java script function.
The first box on the page allows you to select a country based on option values, once selected a javascript function runs which drills into an array and picks up all the resorts in that country. Fine.
When an option from the resorts box is selected a similar event should happen which picks up the sub resorts from another array based on resort selection. This is not happening. It doesn’t seem to pass the resort value to the js function.

An example which should work is Spain, Benidorm.

Any help appreciated

Thanks

options-change.js
function ChangeResortOptions(Country,Resort,CountryResort)
{

var arrLen = eval(CountryResort + “.length”);
var strKey1 = trim_string(eval(Country + “.options[” + Country + “.selectedIndex].value”));

eval(Resort + “.options.length = 0”);
eval(Resort + “.options[0] = new Option(‘Select a resort…’,’?’)”);

var listLen = 1;

for (var i=0; i < arrLen; i++)
{
if (eval(CountryResort + “[0]”) == trim_string(strKey1))
{
eval(Resort + “.options[listLen] = new Option(” + CountryResort + “[1])”);
listLen = listLen + 1;
}
}

eval(Resort + “.options[0].selected = true”);

if (document.layers)
{
history.go(0);
}

}

function ChangeSubResortOptions(Resort,SubResort,ResortSubResort)
{

var arrLen = eval(ResortSubResort + “.length”);
var strKey1 = trim_string(eval(Resort + “.options[” + Resort + “.selectedIndex].value”));

eval(SubResort + “.options.length = 0”);
eval(SubResort + “.options[0] = new Option(‘Select a town/sub resort…’,’?’)”);

var listLen = 1;

for (var i=0; i < arrLen; i++)
{
if (eval(ResortSubResort + “[0]”) == trim_string(strKey1))
{
eval(SubResort + “.options[listLen] = new Option(” + ResortSubResort + “[1])”);
listLen = listLen + 1;
}
}

eval(SubResort + “.options[0].selected = true”);

if (document.layers)
{
history.go(0);
}
if (listLen > 1)
{
document.addholidayform.SubResort.style.visibility = ‘visible’;
}

}

function f_trimmer(thisstring)
{
while(thisstring.charAt(0)==” “)
{
thisstring=thisstring.substring(1,thisstring.length)
}
thisstring=r_trimmer(thisstring)
return thisstring
}

function r_trimmer(thisstring)
{
while(thisstring.charAt(thisstring.length-1)==” “)
{
thisstring=thisstring.substring(0,(thisstring.length-1))
}
return thisstring
}

function trim_string(thisstring)
{
return f_trimmer(thisstring)
}

function Unhide_Box(box_name)
{
eval(box_name + “= ‘visible'”);
}

html code:
<script language=”JavaScript1.1″ src=”scripts/options-change.js”></script>
<html>
<head>
<title>Meet on holiday – Welcome</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>

<script language=”JavaScript”>
var resortArray = new Array();resortArray[0] = new Array (‘Andorra’,’Arinsal-Pal’);
resortArray[1] = new Array (‘Andorra’,’Pas de la Casa’);
resortArray[2] = new Array (‘Andorra’,’Soldeu El Tarter’);
resortArray[3] = new Array (‘Australia’,’Sydney’);
resortArray[4] = new Array (‘Austria’,’Kitzbuhel’);
resortArray[5] = new Array (‘Austria’,’Niederau’);
resortArray[6] = new Array (‘Austria’,’Saalbach’);
resortArray[7] = new Array (‘Austria’,’Salzburg’);
resortArray[8] = new Array (‘Austria’,’Soll’);
resortArray[9] = new Array (‘Austria’,’Vienna’);
resortArray[10] = new Array (‘Austria’,’Zell Am See’);
resortArray[11] = new Array (‘Bahamas’,’Nassau’);
resortArray[12] = new Array (‘Balearic Islands’,’Formentera’);
resortArray[13] = new Array (‘Balearic Islands’,’Ibiza’);
resortArray[14] = new Array (‘Balearic Islands’,’Majorca’);
resortArray[15] = new Array (‘Balearic Islands’,’Menorca’);
resortArray[16] = new Array (‘Belgium’,’Brussels’);
resortArray[17] = new Array (‘Bermuda’,’Hamilton’);
resortArray[18] = new Array (‘Brazil’,’Costa do Sauipe’);
resortArray[19] = new Array (‘Brazil’,’Rio de Janeiro’);
resortArray[20] = new Array (‘Bulgaria’,’Albena’);
resortArray[21] = new Array (‘Bulgaria’,’Golden Sands’);
resortArray[22] = new Array (‘Bulgaria’,’Nessebur’);
resortArray[23] = new Array (‘Bulgaria’,’St Konstantin’);
resortArray[24] = new Array (‘Spain’,’Benidorm’);
resortArray[25] = new Array (‘Spain’,’Costa Blanca’);
resortArray[26] = new Array (‘Spain’,’Costa Brava’);
resortArray[27] = new Array (‘Spain’,’Costa del Sol’);
resortArray[28] = new Array (‘Spain’,’Costa Dorada’);
resortArray[29] = new Array (‘Spain’,’Madeira’);
resortArray[30] = new Array (‘Spain’,’Madrid’);
resortArray[31] = new Array (‘Spain’,’Seville’);

</script>

<script language=”JavaScript”>
var subresortArray = new Array();subresortArray[0] = new Array (‘Benidorm’,’Resort1′);
subresortArray[1] = new Array (‘Costa Dorada’,’Resort2′);

</script>

<link rel=”stylesheet” href=”styles.css” type=”text/css”>
</head>

<body bgcolor=”#9966cc” text=”#000000″ leftmargin=”0″ topmargin=”0″ >
<div align=”center”>
<table width=”730″ border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center” height=”500″>
<tr>
<td width=”20″ height=”20″ background=”images/border-tl.gif”> </td>
<td background=”images/border-t.gif” height=”20″> </td>
<td width=”18″ height=”20″ background=”images/border-tr.gif”> </td>
</tr>
<tr>
<td background=”images/border-l.gif” width=”20″> </td>
<td bgcolor=”#FFFFFF” valign=”top”>
<table width=”680″ border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td colspan=”3″>
<table width=”550″ border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td valign=”top”>
<p> </p>
<table width=”350″ border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td background=”images/searchbox-l.gif”> </td>

<td bgcolor=”#ccccff” valign=”middle”><table width=”290″ border=”0″ cellspacing=”0″
cellpadding=”0″ align=”center”>

<form name=”addholidayform” method=”post”>
<tr>
<td><p align=”center”>
<select id=”nnCountry”
onChange=”ChangeResortOptions(‘document.addholidayform.nnCountry’,
‘document.addholidayform.nnResort’,’resortArray’);”
name=”nnCountry”>
<option value=”novalue” selected>
Select a country…</option>
<option value=’Andorra’>Andorra</option><option value=’Australia’>Australia</option><option value=’Austria’>Austria</option><option value=’England’>England</option><option value=’Spain’>Spain</option><option value=’Sweden’>Sweden</option><option value=’Yugoslavia’>Yugoslavia</option></select></p></td>
</tr>
<tr>
<td><p align=”center”>
<select id=”nnResort”
onChange=”ChangeSubResortOptions(‘document.addholidayform.nnResort’,
‘document.addholidayform.SubResort’,’subresortArray’);”
name=”nnResort”>
<option value=”novalue” selected>
Select a resort…</option>
</select></p></td>
</tr>
<tr>
<td><p align=”center”>
<select name=”SubResort” id=”SubResort” style=”visibility:hidden”>
<option value=”novalue” selected>
Select a town/sub resort…</option>
</select></p></td>
</tr>
</form>
</table> </td>
<td background=”images/searchbox-r.gif”> </td>
</tr>
</table>
<p> </p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td background=”images/border-r.gif” width=”18″> </td>

</tr>
</table>
<p align=”center”>
</div></body>
</html>

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@AdamBrillJan 16.2003 — I attached a .zip file. Try it; I think it should work for you. Let me know. BTW, next time you post code, put it inside the [code] tags.

[upl-file uuid=d48d8caf-5ed1-4b9f-8241-76c3ace12a9e size=2kB]site.zip[/upl-file]
Copy linkTweet thisAlerts:
@rbeech23authorJan 16.2003 — Thanks but this doesn't work. Did it work for you ?

The root of the problem is that the value of resort doesn't seem to get passed to the function and therefore cannot be extracted from the array.

What puzzles me is that the same code works when I pass the value of country.

??

Any further help appreciated

Thanks
Copy linkTweet thisAlerts:
@AdamBrillJan 16.2003 — Sorry, I missunderstood what you wanted. Where ever you want to get the value of the one selected, do this:
<i>
</i>thevalue=document.addholidayform.nnResort.options[document.addholidayform.nnResort.options.selectedIndex].innerText;

then, thevalue will have the text that is selected in the second box. Is that what you wanted?
Copy linkTweet thisAlerts:
@rbeech23authorJan 16.2003 — Thanks a lot, that works.

I'm still puzzled why it works for one text button and not another but am happy.

Would you know how I reference a form name within the function ?

ie document.addholidayform.Resort, I also have another form doing a similar thing with a different form name but would like to use the same function if possible. Some kind of loop round the elements would suffice then an if that picks out the form name and acts accordingly

Thanks again
Copy linkTweet thisAlerts:
@AdamBrillJan 16.2003 — hmm... Like this?

top.frames[0].document.addholidayform.Resort

would take the first frame

top.frames[1].document.addholidayform.Resort

takes the second one, and so on... Or, you could do it like this:

top.framename.document.addholidayform.Resort

Is that what you wanted?
Copy linkTweet thisAlerts:
@rbeech23authorJan 16.2003 — Not quite, it's the addholidayform reference I want to avoid, this means the function can be applied on a different page and a different form which has similar fields

ie. top.framename.document.addholidayform.Resort I'd like to reference as top.framename.document.<formnamevariable>.resort.

What I could do then is pass this as the fourth argument to the function(s)

Hope you can help

Thanks
Copy linkTweet thisAlerts:
@AdamBrillJan 16.2003 — Like this:

top.frames[0].document.forms[0].Resort.value

Is that what you wanted? If you don't want to type Resort in there either, try this:

elements=top.frames[0].document.getElementsByTagName('input');

TheValue=elements[0].value;

That will get the first one thing in the form with the input tag and put it in the TheValue variable. So, I don't know how you want to do it, but those are a few options. Let me know if one of those work...
×

Success!

Help @rbeech23 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...