/    Sign up×
Community /Pin to ProfileBookmark

arrays and forms, withour defining too many vars

Hi, I’m fairly new (well, extremely new to javascript) and i’m trying to create a very simple form that will show some information based on what was selected in one box only. That’s easy. But i have a LOT of options (it’s a contact list).

I’ve done a [i]tiny[/i] amount of programming in java so was confident of using arrays and thought it’d be a good idea. But now i’m having problems.

When i load the page i get two error reports: one is simply a missing ; but i don’t think that’s a biggie…could be wrong. The major problem seems to be that on line 62 – the <input name=”num”… line – its expecting an object and i clearly haven’t given it one somehow. Am i being too pure java in my thought processes. I’ve cut the script down to the basics and set it below…any suggestions would be massively appreciated.

Cheers
Al

p.s. there is also the problem that in the select box (name) it comes up as “numarrary[i]” instead of the value of that entry in the array. I’m guessing that’s just me not doing something right moving from java to jscript…or something simplish.

[code]

<head>
<script>

function numReturn()
{

var numarray = array();
// set all the entries to their appropriate values. Name, then number,then email prefix, then email suffix
numarray[0] = “Joe Bloggs”;
numarray[1] = “0800 000 0000”;
numarray[2] = “joe.bloggs”;
numarray[3] = “host.com”;

numarray[4] = “someone else”;
numarray[5] = “12345678”;
numarray[6] = “anyone”;
numarray[7] = “anywho.com”;

document.telNo.name.selectedIndex;
document.telNo.num.value;

if (document.telNo.name.selectedIndex == 1) {document.telNo.num.value = “Tel no:”+numarray[1]+”, “+numarray[2]+”@”+numarray[3];}
if (document.telNo.name.selectedIndex == 2) {document.telNo.num.value = “Tel no:”+numarray[5]+”, “+numarray[6]+”@”+numarray[7];}

If (document.telNo.name.selectedIndex == “x” || document.telNo.date.selectedIndex == “y”) return;
}

</script>
</head>

<body>

<form name=”telNo”>
<table>
<tr>
<td >&nbsp;<b>Telephone Number Retreival</b></font>
</td>
</tr>
<tr>
<td>
<b>&nbsp;Name&nbsp;</b>
</td>
<td rowspan=”2″>
<b>&nbsp;&nbsp;<b>:</b>&nbsp;</b>
</td>
<td>
<b>&nbsp;Tel No.&nbsp;</b>
</td>
</tr>
<tr>
<td>
&nbsp;<select name=”name” onChange=”document.telNo.num.focus();”>
var numarray = dataarray();
<option value=”x” selected>&lt; Select &gt;&nbsp;</option>

<option value=”1″>numarray[0]</option>
<option value=”2″>numarray[4]</option>
</select>
</td>

<td>
&nbsp;<input name=”num” size=”12″ value=”??” onFocus=”numReturn();” onBlur=”reset();” />&nbsp;
</td>
</tr>
</table>
</form>
</body>

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mg8Sep 14.2003 — I believe first off, you need this:

<script [B]language="javascript" type="text/javascript">[/B]

That might fix some problems :p
Copy linkTweet thisAlerts:
@David_HarrisonSep 14.2003 — OK, this line:

if(document.telNo.name.selectedIndex == "x" || document.telNo.date.selectedIndex == "y")

should look like this:

if(document.telNo.name.options[document.telNo.name.selectedIndex].value == "x" || document.telNo.date.options[document.telNo.date.selectedIndex].value == "y")

Also you shouldn't really have name="name", change it to something else that means the same thing, otherwise one time you might bumb into a key word that causes errors in your JavaScript, (eg. naming a variable var).

And finally you script tags should look like this:

<script type="text/javascript"><!--

//--></script>

The <!-- is an opening HTML comment tag so that browsers with JS disabled don't mis read you JS as HTML.

The // is a JS comment tag because JS doesn't understand the closing HTML comment tag.

The --> is a closing HTML comment tag so that browsers with JS disabled will start to read everything after it as HTML again.

Note that browsers with JS disabled will simply ignore the actual script tags themselves as they do not understand them.

Also note that if you are going to use --> in your script, eg:

document.write("-->");

Instead use:

document.write("--&gt;");
×

Success!

Help @theuberpuppy 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.18,
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,
)...