/    Sign up×
Community /Pin to ProfileBookmark

Input text field loop problem

I’m struggling with JavaScript, and I can’t seem to figure out this problem: In the code below, I want the user to type in a name in a text field and make selections from the checkboxes and dropdown. When they hit the Submit button, the generated text should show.

There are two problems here:

  • 1.

    The name value is repeated as many times as the number of checkboxes selected (ie if you select all three checkboxes, you’ll see the name value three times in the generated text)

  • 2.

    The generated text only displays for an instant, before the page refreshes – if you click the back button in your browser, the generated text is there. This seems to be because of my use of the <form> tags, but I can’t figure out how to get the name value without using document.myForm.First.value which seems to require the use of <form> tags…

  • [CODE]<html>
    <head>
    <title>Text Generator</title>

    <script type=”text/javascript”>

    function GetMDDselections(ids) {
    // document.getElementById(ids).size = 1; // reduce screen size if desired
    var picked = ”;
    for (i=0; i<document.getElementById(ids).options.length; i++) {
    if (document.getElementById(ids).options[i].selected == true) { picked += i+’,’; }
    } // document.getElementById(ids).selectedIndex = -1; // reset list
    return picked;
    }

    function UpdateSign() {
    var opts = GetMDDselections(‘SignOff’);
    var opt_picked = new Array();
    opt_picked = opts.split(‘,’);
    var txt = ”; // txt = opts;
    for (var i=0; i<opt_picked.length-1; i++) {
    txt = ‘nn’+document.getElementById(‘SignOff’).options[opt_picked[i]].value;
    }
    return txt;
    }

    function UpdateStmt() {
    var txt = UpdateSign(‘SignOff’);
    for (var i=1; i<=3; i++) {
    if (document.getElementById(‘Stmt’+i).checked == true) {
    txt = “Dear “+document.myForm.First.value+”, “+document.getElementById(‘Stmt’+i).value+txt;
    }
    }
    document.getElementById(‘TArea’).value = txt;
    }

    function Tgl(flag) {
    if (flag == true) {
    document.getElementById(‘divPick’).style.display = ‘none’;
    document.getElementById(‘divTA’).style.display = ‘block’;
    } else {
    document.getElementById(‘divPick’).style.display = ‘block’;
    document.getElementById(‘divTA’).style.display = ‘none’;
    }
    }
    </script>

    </head>
    <body>

    <div id=”divPick” style=”display:block”>
    <form name=”myForm”>
    First name of the person<br />
    <input name=”First” type=”text” onChange=”UpdateStmt()” />

    <br /><br /><br />
    <input id=”Stmt1″ type=”checkbox” onChange=”UpdateStmt()” value=”Hello. “>Hello.<br />
    <input id=”Stmt2″ type=”checkbox” onChange=”UpdateStmt()” value=”How are you? “>How are you?<br />
    <input id=”Stmt3″ type=”checkbox” onChange=”UpdateStmt()” value=”The weather is great here. “>The weather is great here.<br />
    <br /><br /><br />
    Sign-off line<br/>
    <select id=”SignOff” onChange=”UpdateStmt()” size=”1″>
    <option value=””>- Select -</option>
    <option value=”Sincerely,”> Sincerely</option>
    <option value=”Yours truly,”> Yours truly</option>
    </select>
    <br />
    <button id=”goBack” onClick=”Tgl(true);”>Submit</button>
    </div>
    <p />
    <div id=”divTA” style=”display:none”>
    <textarea id=”TArea” rows=”30″ cols=”70″ readonly></textarea>
    <br />
    <button id=”goBack” onClick=”Tgl(false);”>Make changes</button>
    </div>
    </form>
    </body>
    </html> [/CODE]

    Example of the code above: [URL=”http://parkour.ca/textgenerator.html”]http://parkour.ca/textgenerator.html[/URL]

    Example that does what I want, except does not display the name value: [URL=”http://parkour.ca/textgenerator2.html”]http://parkour.ca/textgenerator2.html[/URL]

    I will also need to append a name to the end of the generated text, also from an input text field, so this is really bugging me. I tried to figure out how to get values from a selection of radio buttons on the same page, but I gave up on that after a few days, as I got nowhere with it. Any suggestions about any part of this, to help ease this frustration, would be greatly appreciated!

    Edit: Also, for some reason the checkbox values end up in reverse order in the generated text – any clues as to what I screwed up there?

    to post a comment
    JavaScript

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @samliewNov 18.2007 — [code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Text Generator</title>
    <script type="text/javascript">
    function UpdateStmt() {
    // Set the starting of the message
    var txt = "Dear " + document.myForm.First.value + ",nn";

    // Using the changes to the loop below, you can add in more checkboxes and it will still work
    var checkboxes = document.getElementsByName('Stmt'); // Gets ALL checkboxes with the name: 'Stmt'
    for (i=0; i<checkboxes.length; i++) { // Loop will run for all checkboxes
    if (checkboxes[i].checked == true) { // Check if checkbox is selected
    txt += checkboxes[i].value + "n"; // Adds checkbox value to 'txt'
    }
    }

    txt += "n"+document.myForm.SignOff.options[document.myForm.SignOff.selectedIndex].value + "n"; // Adds select value to 'txt'
    txt += document.myForm.yourName.value + "n"; // Adds your name to 'txt'
    document.getElementById('TArea').value = txt; // Sets value of textarea
    }

    function Tgl(flag) {
    if (flag == true) {
    document.myForm.style.display = 'none';
    document.getElementById('divTA').style.display = 'block';
    } else {
    document.myForm.style.display = 'block';
    document.getElementById('divTA').style.display = 'none';
    }
    }
    </script>
    </head>
    <body>
    <form name="myForm" action="" onsubmit="return false;" style="display:inline;">
    <p>First name of the person
    <input name="First" type="text" />
    </p>
    <p>
    <input name="Stmt" type="checkbox" value="Hello. " />
    Hello.<br />
    <input name="Stmt" type="checkbox" value="How are you? " />
    How are you?<br />
    <input name="Stmt" type="checkbox" value="The weather is great here. " />
    The weather is great here.</p>
    <p> Sign-off line<br/>
    <select name="SignOff" size="1">
    <option value="">- Select -</option>
    <option value="Sincerely,"> Sincerely</option>
    <option value="Yours truly,"> Yours truly</option>
    </select>
    </p>
    <p>Your name
    <input name="yourName" type="text" />
    </p>
    <p>
    <input type="button" onclick="UpdateStmt();Tgl(true);" value="Submit" />
    </p>
    </form>
    <div id="divTA" style="display:none;">
    <textarea name="TArea" id="TArea" rows="30" cols="70" readonly="readonly"></textarea>
    <br />
    <button id="goBack" onclick="Tgl(false);">Make changes</button>
    </div>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @photodiaristauthorNov 18.2007 — That's PERFECT! Thanks Sam, you're my hero! Extremely helpful! ?
    Copy linkTweet thisAlerts:
    @samliewNov 18.2007 — The reverse order is due to your code:
    txt = "Dear "+document.myForm.First.value+", "+document.getElementById('Stmt'+i).value[B][COLOR="Red"]+txt[/COLOR][/B];

    You should use either:
    txt = [B][COLOR="red"]txt +[/COLOR][/B] "Dear "+document.myForm.First.value+", "+document.getElementById('Stmt'+i).value;
    or this
    txt [B][COLOR="red"]+=[/COLOR][/B] "Dear "+document.myForm.First.value+", "+document.getElementById('Stmt'+i).value;
    Copy linkTweet thisAlerts:
    @samliewNov 18.2007 — That's PERFECT! Thanks Sam, you're my hero! Extremely helpful! ?[/QUOTE]

    Glad I could help you ?
    ×

    Success!

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