/    Sign up×
Community /Pin to ProfileBookmark

Simple code help needed

[COLOR=”Magenta”]Hi. I normally use html but am dabbling in javascript now. However, I cannot wrap my head around the simplest way to do the following and am hoping someone could suggest some code that would work?

I basicly need to create some variables and set them equal to some text. The web form will also have a button which will appear on the page. When that button is clicked,the contents of the text fields will show up in an alert box. I will need at least four text fields.

Thank you.:o[/COLOR]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMay 02.2010 — maybe use something like this:

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

function showText() {
var str = '';
var textA = new Array();
textA = ['text_1',
'text_2',
'text_3',
'text_4'];

for(var i in textA) {
str = str + textA[i] + "n";
}
alert(str);
}

//-->
</script>

</head>
<body>

<div>
<button onclick="showText();">Show text fields</button>
</div>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@helpagirloutauthorMay 02.2010 — Thank you, that is simple and perfect. You have a great day.. you just made mine! ?
Copy linkTweet thisAlerts:
@tirnaMay 02.2010 — no problem - always happy to help a damsell in distress ?
Copy linkTweet thisAlerts:
@NicTltMay 02.2010 — At the risk of sounding pedantic, where you have
[CODE]var textA = new Array();
textA = ['text_1',
'text_2',
'text_3',
'text_4'];[/CODE]


you only need
[CODE]var textA = ['text_1',
'text_2',
'text_3',
'text_4'];[/CODE]
Copy linkTweet thisAlerts:
@tirnaMay 02.2010 — yes I know, but I prefer to put the extra line in, especially for newbies, to make it absolutely clear what is happening.
Copy linkTweet thisAlerts:
@NicTltMay 02.2010 — How does that help?
Copy linkTweet thisAlerts:
@spinnykidMay 02.2010 — How does that help?[/QUOTE]

Not every body know's what a variable is, and by showing the variable creation, they have a little more of an idea, and can do a little more reaserch on if the want...

e.g.

Searching for "new Array();" would proberbly give better results than "['text_1',

'text_2',

'text_3',

'text_4'];"
Copy linkTweet thisAlerts:
@tirnaMay 03.2010 — How does that help?[/quote]

You would have to ask each individual if it helps them or not. Obviously it will help some newbies and not others.

In the mean time I agree with spinnykid's explanation and I will continue to add the extra line in the future whether it helps everyone or just a few ?.
Copy linkTweet thisAlerts:
@Declan1991May 03.2010 — In the mean time I agree with spinnykid's explanation and I will continue to add the extra line in the future whether it helps everyone or just a few ?.[/QUOTE]

It's extremely misleading, because you are creating two arrays, and overwriting the first with the latter. It might lead people to believe you have to do it that way.
Copy linkTweet thisAlerts:
@tirnaMay 03.2010 — imo just having something like ['val1','val2'] can be very unclear to many newbies. That is why I will continue to include new Array().

It's just my preferred way of doing things. I'm not saying you must do it that way.
Copy linkTweet thisAlerts:
@NicTltMay 03.2010 — A simple comment would serve the same purpose but without the dire consequences.
Copy linkTweet thisAlerts:
@tirnaMay 03.2010 — I don't see any consequences as dire.
×

Success!

Help @helpagirlout 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...