Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2011, 05:32 AM   PM User | #1
theatomicdude
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
theatomicdude is an unknown quantity at this point
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....
theatomicdude is offline   Reply With Quote
Old 01-06-2011, 08:29 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
theatomicdude (02-16-2011)
Old 01-06-2011, 11:08 PM   PM User | #3
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
var r = document.getElementById("list1").options[document.getElementById("list1").selectedIndex].value;
Lerura is offline   Reply With Quote
Old 01-07-2011, 07:34 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-08-2011, 03:07 AM   PM User | #5
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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
Lerura is offline   Reply With Quote
Old 02-16-2011, 04:42 PM   PM User | #6
theatomicdude
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
theatomicdude is an unknown quantity at this point
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.
theatomicdude is offline   Reply With Quote
Old 02-17-2011, 03:25 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 02-17-2011 at 03:37 AM..
rnd me is offline   Reply With Quote
Old 02-17-2011, 03:53 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
rnd me (02-17-2011)
Reply

Bookmarks

Tags
automatic, forms, generator, letter

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:49 PM.


Advertisement
Log in to turn off these ads.