/    Sign up×
Community /Pin to ProfileBookmark

Populating textboxes with arrays

Hi

In my webpage there 3 text boxes (txt1, txt2, txt3) & a button. My Javascript has 2 arrays:
var s = new Array(1, 2);
var m = new Array(3, 4);

How do I do the Javascript & HTML so that if I enter ‘s’ into txt1 & click the button the values ‘1’ & ‘2’ in the array shows in txt2 & txt3? Same with ‘m’ array

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyApr 28.2008 — Show us what you have worked out so far for your assignment then we can point to in the right direction.
Copy linkTweet thisAlerts:
@A1ien51Apr 28.2008 — A better way of doing it would be to store the lines in an object and reference it that way.

<i>
</i>var data = {
s : [1, 2],
m : [3, 4]
}


function foo(){

var userInput = document.forms[0].txt1.value;
var vals = data[userInput];
var val1="";
var val2="";

if(vals==null){
val1 = vals[0];
val2 = vals[1];
}

document.forms[0].txt2.value = val1;
document.forms[0].txt3.value = val2;

}



Eric
Copy linkTweet thisAlerts:
@OahgnegauthorApr 28.2008 — A1ien51 I tried your script but I got this error?

'document.forms.0.txt1 is null or not an object'
Copy linkTweet thisAlerts:
@A1ien51Apr 28.2008 — Do you have a form tag? Do you have a name set on your textboxes? Show us what you have done.

Eric
Copy linkTweet thisAlerts:
@OahgnegauthorApr 28.2008 — Do you have a form tag?

Eric[/QUOTE]

...So that was what's missing. But what URL do I put in the action attribute?

Here's what I have so far:
[CODE]<HTML>
<HEAD>
<TITLE>JavaScript</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
var data = {
s : [1, 2],
m : [3, 4]
}

function foo(){

var userInput = document.forms[0].txt1.value;
var vals = data[userInput];
var val1="";
var val2="";

if(vals==null){
val1 = vals[0];
val2 = vals[1];
}

document.forms[0].txt2.value = val1;
document.forms[0].txt3.value = val2;

}
</script>
</HEAD>
<BODY bgcolor="blue">
<input type = “text” name=”txt1” id="txt1" value="" /><br>
<input type="button" value="button" name="button" onclick="return foo()"/><br>
<input type = “text” name=”txt2” id="txt2" value="" /><br>
<input type = “text” name=”txt3” id="txt3" value="" />
</BODY>
</HTML>[/CODE]
×

Success!

Help @Oahgneg 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.17,
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,
)...