PDA

View Full Version : Filling form by clicking on a link, problem with arrays


omewous
03-26-2008, 10:46 PM
Hi all!

I've got a problem. I'm writing a script that should fill a form by clicking on a link and automatically fill in the values. I want it to work with an array, but I think I'm doing something wrong. Anyway here's the code:


<script language=javascript>

var regio = new Array();
var acc = new Array();
var soort = new Array();

function FillForm()
{
document.formulier.regio[1].value = "71";
document.formulier.acc[1].value = "1038";
document.formulier.soort[1].value = "23";
}
</script>
<a href="javascript:FillForm()">Fill it!</a>
<form name=formulier>

<SELECT NAME="regio[1]" class="input" style = "width:150px">
<OPTION VALUE="0" >Regio</OPTION>
<OPTION VALUE="71">Palma de Mallorca</OPTION>
<OPTION VALUE="73">Noordwestkust</OPTION>
<OPTION VALUE="74">Pollensa en omgeving</OPTION>
<OPTION VALUE="75">Noordoost Mallorca</OPTION>
<OPTION VALUE="76">Oost Mallorca</OPTION>
<OPTION VALUE="77">Zuid Mallorca</OPTION>
<OPTION VALUE="78">Mallorca interieur</OPTION>
</SELECT>

<SELECT NAME="acc[1]" class="input" style = "width:250px">
<OPTION VALUE="0" >Accommodatie</OPTION>
<OPTION VALUE="1038">accomodatie 1038</OPTION>
<OPTION VALUE="345">tweede</OPTION>
<OPTION VALUE="234">derde</OPTION>
<OPTION VALUE="8900">vierde</OPTION>
</SELECT>

<SELECT NAME="soort[1]" class="input" style = "width:150px">
<OPTION VALUE="0" >Soort verblijf</OPTION>
<OPTION VALUE="23">eerste</OPTION>
<OPTION VALUE="24">tweede</OPTION>
<OPTION VALUE="25">derde</OPTION>
<OPTION VALUE="26">vierde</OPTION>
</SELECT>

</form>


This doesn't work.

But if I don't use arrays it does work:
<script language=javascript>
function FillForm()
{
document.formulier.regio.value = "71";
document.formulier.acc.value = "1038";
document.formulier.soort.value = "23";
}
</script>
<a href="javascript:FillForm()">Vullen!</a>
<form name=formulier>

<SELECT NAME="regio" class="input" style = "width:150px">
<OPTION VALUE="0" >Regio</OPTION>
<OPTION VALUE="71">Palma de Mallorca</OPTION>
<OPTION VALUE="73">Noordwestkust</OPTION>
<OPTION VALUE="74">Pollensa en omgeving</OPTION>
<OPTION VALUE="75">Noordoost Mallorca</OPTION>
<OPTION VALUE="76">Oost Mallorca</OPTION>
<OPTION VALUE="77">Zuid Mallorca</OPTION>
<OPTION VALUE="78">Mallorca interieur</OPTION>
</SELECT>

<SELECT NAME="acc" class="input" style = "width:250px">
<OPTION VALUE="0" >Accommodatie</OPTION>
<OPTION VALUE="1038">accomodatie 1038</OPTION>
<OPTION VALUE="345">tweede</OPTION>
<OPTION VALUE="234">derde</OPTION>
<OPTION VALUE="8900">vierde</OPTION>
</SELECT>

<SELECT NAME="soort" class="input" style = "width:150px">
<OPTION VALUE="0" >Soort verblijf</OPTION>
<OPTION VALUE="23">eerste</OPTION>
<OPTION VALUE="24">tweede</OPTION>
<OPTION VALUE="25">derde</OPTION>
<OPTION VALUE="26">vierde</OPTION>
</SELECT>

</form>


Personally I think I'm doing something wrong in the syntaxis of this part of the code:

document.formulier.regio[1].value = "71";
document.formulier.acc[1].value = "1038";
document.formulier.soort[1].value = "23";

I hope you guys can help me

Thanks in advance!

Wouter van der Meij

shyam
03-27-2008, 12:04 AM
your problem is not with the arrays. the regio, acc and soort arrays declared in javascript are not the same when used within the name attribute

here's how u can reference the selects even if u have []'s in their name
document.formulier['regio[1]'].value = "71";
document.formulier['acc[1]'].value = "1038";
document.formulier['soort[1]'].value = "23";

omewous
03-27-2008, 11:14 PM
Thanks it works!