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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post How do automatic letter generators work?

    I need to make something that will automatically insert values into a letter based on user input, just like this one right here. please tell me how it works....

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    It is no more than a series of select lists from which the user may choose an option.


    Code:
    <select id = "list1">
    <option value = "">Choose a recipient</option>
    <option value = "Father">Father</option>
    <option value = "Mother">Mother</option>
    <option value = "Grandma">Grandma</option>
    </select>
    
    <input type = "button" value = "Generate Letter" onclick = "generate()">
    <br><br>
    <div id = "letter">
    </div>
    
    <script type = "text/javascript">
    
    function generate() {
    var r = document.getElementById("list1").value;
    if (r!="") {
    var message = "Dear " + r + ",";
    document.getElementById("letter").innerHTML = message;
    }
    }
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • Users who have thanked Philip M for this post:

    theatomicdude (02-16-2011)

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    var r = document.getElementById("list1").options[document.getElementById("list1").selectedIndex].value;

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by lerura View Post
    var r = document.getElementById("list1").options[document.getElementById("list1").selectedIndex].value;
    No - var r = document.getElementById("list1").value; is just fine.

  • #5
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    Quote Originally Posted by Philip M View Post
    No - var r = document.getElementById("list1").value; is just fine.
    Well - I have always used, and never questioned the way I learned it.
    Nice to know that there is a shorter and easier way to do it

  • #6
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    It is no more than a series of select lists from which the user may choose an option.


    Code:
    <select id = "list1">
    <option value = "">Choose a recipient</option>
    <option value = "Father">Father</option>
    <option value = "Mother">Mother</option>
    <option value = "Grandma">Grandma</option>
    </select>
    
    <input type = "button" value = "Generate Letter" onclick = "generate()">
    <br><br>
    <div id = "letter">
    </div>
    
    <script type = "text/javascript">
    
    function generate() {
    var r = document.getElementById("list1").value;
    if (r!="") {
    var message = "Dear " + r + ",";
    document.getElementById("letter").innerHTML = message;
    }
    }
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    thanks dude, i havent tried it yet but i'll let you know.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    Quote Originally Posted by Philip M View Post
    No - var r = document.getElementById("list1").value; is just fine.
    are you sure about that always and everywhere?

    EDIT
    i know there's got to be a reason i've been doing that stupid x.options [x.selectedIndex ] crap for years...


    i'm almost certain that it won't work in IE6, but seeing as IE6 sucks and i don't have a copy of IE6, I'll concede the point.
    makes me wonder how long setting the .value of a select has worked as well...

    If that's the only reason, screw IE6, i'm going for the .value from now on.
    Last edited by rnd me; 02-17-2011 at 03:37 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by rnd me View Post
    are you sure about that always and everywhere?

    EDIT
    i know there's got to be a reason i've been doing that stupid x.options [x.selectedIndex ] crap for years...


    i'm almost certain that it won't work in IE6, but seeing as IE6 sucks and i don't have a copy of IE6, I'll concede the point.
    makes me wonder how long setting the .value of a select has worked as well...

    If that's the only reason, screw IE6, i'm going for the .value from now on.
    No, I am not sure as there are there are a bunch of marginal browsers out there such as Konqueror and Lunescape. But it works in IE (including IE6 ), FF and (I am pretty sure) Opera.

    Edit: Now tested in Opera.
    Last edited by Philip M; 02-17-2011 at 04:20 PM.

  • Users who have thanked Philip M for this post:

    rnd me (02-17-2011)


  •  

    Tags for this Thread

    Posting Permissions

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