Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    "sentance generator"

    I am trying to make a form that will insert strings entered into textfields into a sentance. I've accomplished this, but I don't know how to use a checkbox to toggle an extra, fixed word in the textarea.

    For example, if 'a' was "," & 'b' was "and", the output would be "Beginning, middle and end."

    But how do I make it output "Beginning, middle and end." only if 'c' is left unchecked, and "Beginning, middle and the end." when 'c' IS checked?

    Code:
    <html><head>
    <script type="text/javascript">
    function createtext() {
    output.value= "Beginning"+
    a.value+
    " middle "+
    b.value+
    " end.";
    }
    </script></head><body><form>
    Beginning<br>
    <input type="text" id="a"><br>
    middle<br>
    <input type="text" id="b"><br>
    <input type="checkbox" id="c">the<br>
    end.<br>
    <input type="button" onClick="createtext();" value="submit"><br>
    <textarea id="output"></textarea>
    </form></body></html>
    Thanks in advance.
    Last edited by 7111; 10-18-2006 at 11:36 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Try this:
    Code:
    function createtext() {
     if (document.forms.c.checked) { c=' the '; } else { c=''; }
     output.value= "Beginning"+
      a.value+
      " middle "+
      b.value+c+
      " end.";
    }

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That didn't work, but I got some help from another forum, and the following worked perfectly:
    Code:
    <script type="text/javascript">
    function createtext() {
    if (c.checked == 1) {document.forms['z'].elements['the'].value = "the";}
    else {document.forms['z'].elements['the'].value = " ";}
    output.value= "Beginning" + a.value + " middle " + b.value + " " + the.value + " end.";
    }
    </script>
    Code:
    <form name='z'>
    <input type="hidden" id="the">
    Beginning<br>
    <input type="text" id="a"><br>
    middle<br>
    <input type="text" id="b"><br>
    <input type="checkbox" id="c">the<br>
    end.<br>
    <input type="button" onClick="createtext();" value="CLICK ME"><br>
    <textarea id="output"></textarea>
    </form>
    Last edited by 7111; 10-19-2006 at 06:35 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •