/    Sign up×
Community /Pin to ProfileBookmark

Trying to output text

I’m converting an applet I made into JavaScript (i’m new to it) and am having some troubles. What I’m trying to do is have three textfields filled in. Then, with a click of a button, I want to have certain parts of the inputted text appear in a textarea. Here’s what I have:

[code=html]
<head>
<script type=”text/javascript”>
function do_something(str1,str2,str3)
{
var form1Str1 = form1.field1.value;
var form1Str2 = form1.field2.value;
var form1Str3 = form1.field3.value;

form1.outputArea.value=’Field 1: ‘ + str1 + ‘nField 2: ‘ + str2 + ‘nField 3: ‘ + str3

}
</script>

</head>
<body>
<FORM NAME=”form1″>
<table border=”3″ align=”center” bordercolor=”006600″ bgcolor=”beige”>
<tr>
<td align=”right”>Field 1 <INPUT TYPE=TEXT SIZE=4 NAME=”field1″ MAXLENGTH=3></td>
<td align=”left”><INPUT TYPE=TEXT SIZE=6 MAXLENGTH=5 NAME=”field2″> Field 2</td>
</tr>
<tr>
<td colspan=2 align=”left”>Field 3 <INPUT TYPE=TEXT SIZE=12 MAXLENGTH=11 NAME=”field3″></td>
</tr>
<tr>
<td align=”center” colspan=2><TEXTAREA ID=”outputArea” ROWS=8 COLS=25></TEXTAREA></td>
</tr>
<tr>
<td align=”right”><INPUT TYPE=SUBMIT NAME=”enter” VALUE=”Submit” onClick=”do_something()”></td>
<td align=”left”><INPUT TYPE=RESET NAME=”reset” VALUE=”Reset Fields”></td>
</tr>
</table>
</FORM>
</body>
[/code]

Does is have anything to do with the form method. I don’t know if I should do anything with the method, whether it be post, get, or something else. Any help will be greatly appreciated.

Edit: I guess it would be helpful to tell you what happens when I run this…nothing. All the fields just end up blank. Is there any way for me to run this function and not have all the fields cleared?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoJun 30.2006 — 
  • 1. use document, for example this:


  • var form1Str1 = form1.field1.value;


    should be:

    var form1Str1 = document.form1.field1.value;

  • 2. you don't send the function any parameters, but you have it take in 3 parameters that you try to output.


  • 3. chnage your enter button to type=button instead of type=submit. That is why the page changes.
  • Copy linkTweet thisAlerts:
    @KingCobra220authorJun 30.2006 — 1. use document, for example this:

    var form1Str1 = form1.field1.value;


    should be:

    var form1Str1 = document.form1.field1.value;

  • 2. you don't send the function any parameters, but you have it take in 3 parameters that you try to output.


  • 3. chnage your enter button to type=button instead of type=submit. That is why the page changes.
  • [/QUOTE]


    Well thanks. That answered 90% of my questions. I changed some stuff and got this:
    [code=html]
    <head>
    <script type="text/javascript">
    function do_something(form1str1,form1str2,form1str3)
    {
    var form1Str1 = document.form1.field1.value;
    var form1Str2 = document.form1.field2.value;
    var form1Str3 = document.form1.field3.value;

    document.form1.outputArea.value='Field 1: ' + form1str1 + 'nField 2: ' + form1str2 + 'nField 3: ' + form1str3

    }
    </script>

    </head>
    <body>
    <FORM NAME="form1">
    <table border="3" align="center" bordercolor="006600" bgcolor="beige">
    <tr>
    <td align="right">Field 1 <INPUT TYPE=TEXT SIZE=4 NAME="field1" MAXLENGTH=3></td>
    <td align="left"><INPUT TYPE=TEXT SIZE=6 MAXLENGTH=5 NAME="field2"> Field 2</td>
    </tr>
    <tr>
    <td colspan=2 align="left">Field 3 <INPUT TYPE=TEXT SIZE=12 MAXLENGTH=11 NAME="field3"></td>
    </tr>
    <tr>
    <td align="center" colspan=2><TEXTAREA ID="outputArea" ROWS=8 COLS=25></TEXTAREA></td>
    </tr>
    <tr>
    <td align="right"><INPUT TYPE=BUTTON NAME="enter" VALUE="Submit" onClick="do_something()"></td>
    <td align="left"><INPUT TYPE=RESET NAME="reset" VALUE="Reset Fields"></td>
    </tr>
    </table>
    </FORM>
    </body>
    [/code]


    but when I enter text and hit enter I get:

    Field 1: undefined

    Field 2: undefined

    Field 3: undefined

    No errors are thrown at me so I'm at a loss for what to do.

    Edit to close thread: I figured it out. Not all the strings were in the same syntax.
    Copy linkTweet thisAlerts:
    @konithomimoJun 30.2006 — That is because you are declaring your variables twice. Change this:

    function do_something(form1str1,form1str2,form1str3)

    {


    to this:

    function do_something()

    {
    ×

    Success!

    Help @KingCobra220 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.20,
    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,
    )...