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 02-12-2013, 08:16 PM   PM User | #1
stickyStan
New to the CF scene

 
Join Date: Feb 2013
Location: NYC
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
stickyStan is an unknown quantity at this point
Question Using two functions at once is giving me difficulty..

I want to ask the user to input two functions. I want the user to input both there name and the time of day it is. (i.e. Morning, Evening, Night). So the message reads, for example. "Good Morning David". This simple task is giving me difficulty for I am new to Javascript. Any help with the code would be greatly appreciated.

Thank You!
stickyStan is offline   Reply With Quote
Old 02-12-2013, 08:23 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
You don't need two functions; one will do with both inputs.

Code:
<input id="yourName">
<select id="timeOfDay">
<option value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Evening">Evening</option>
</select>
<script>
function sayHello(frmObj){
alert("Good " + {select value here} + ", " + {name value here} + ".");
}
</script>
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-12-2013, 09:01 PM   PM User | #3
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
Would it not be easier to display the message based on automatic evaluation the time of day?

Note that alerts are regarded as obsolete and you should prefer to use DOM methods to display the greeting.

Code:
Enter your name <input type = "text" id = "username" onblur = "showMessage()">
<br><br>
<span id = "message"></span>

<script type = "text/javascript">

function showMessage() {
var un = document.getElementById("username").value;

var datetoday = new Date();
var thehour = datetoday.getHours();

var display = "Night";
if ((thehour > 17) && (thehour <23)) {display = "Evening"}
else if (thehour >=12) {display = "Afternoon"}
else if (thehour >= 6) {display = "Morning"};

var greeting = ("Good " + display + " " + un +  "!");
document.getElementById("message").style.display="block";
document.getElementById("message").innerHTML = greeting;
setTimeout("clearIt()", 5000);// 5 seconds
}

function clearIt() {
document.getElementById("message").innerHTML = "";
document.getElementById("message").style.display="none";
}
</script>
"During her imprisonment she gave birth to two girls aged 11 and 15". - BBC Radio 4
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-12-2013 at 09:07 PM..
Philip M is offline   Reply With Quote
Old 02-12-2013, 09:16 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
I think OP is totally new to JavaScript. Your example code (elegant as it is) is most likely out of range. Just a guess.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-12-2013, 09:26 PM   PM User | #5
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 WolfShade View Post
I think OP is totally new to JavaScript. Your example code (elegant as it is) is most likely out of range. Just a guess.
Yes, you may well be right! But at least he can see how the project can be improved.

Your code has the problem that the user cannot select "morning" without selecting something else first. So:-

Code:
<option value="Morning" selected>Morning</option>
And what is supposed to trigger your function sayHello()?

Code:
Enter your name <input type = "text" id="yourName">
<select id="timeOfDay" onchange = "sayHello()">
<option value = "">Choose a time of day</option>
<option value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Evening">Evening</option>
<option value = "Night">Night</option>
</select>

<script type = "text/javascript">

function sayHello() {
var tod = document.getElementById("timeOfDay").value;
var yn = document.getElementById("yourName").value
alert ("Good " + tod + ", " +  yn + "!");
}
</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-12-2013 at 09:39 PM..
Philip M is offline   Reply With Quote
Old 02-12-2013, 09:54 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
Well, I assumed that the OP would be at least familiar enough to add the button for calling the function. I mean, I didn't want to do ALL the leg-work.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-12-2013, 10:02 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
if ((thehour > 17) && (thehour <23)) {display = "Evening"}
What happened to your rule about every statement in JS endng with a semicolon?

I would have written that as
Code:
if ( thehour > 17 && thehour <23 ) {display = "Evening"; }
(I also find excessive parentheses almost as hard to read as too few of them. But that's me.)

And shouldn't that be
Code:
var greeting = "Good " + display + ", " + un +  "!";
Or is the comma there an American but not British requirement?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 02-12-2013 at 10:05 PM..
Old Pedant is offline   Reply With Quote
Old 02-12-2013, 10:06 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 Old Pedant View Post
Code:
if ((thehour > 17) && (thehour <23)) {display = "Evening"}
What happened to your rule about every statement in JS endng with a semicolon?

I would have written that as
Code:
if ( thehour > 17 && thehour <23 ) {display = "Evening"; }
(I also find excessive parentheses almost as hard to read as too few of them. But that's me.)
Hmm. That rule does not apply (I consider) where the whole expression is on one line terminating in the final }
And I try to never leave out the parentheses. But that's me!

Quote:
Originally Posted by Old Pedant View Post
Or is the comma there an American but not British requirement?
Yes, on consideration I think you are probably right. I did put a comma in the code in Post#5.

Edit: I think that both are in fact correct.

In a dialogue, a pause can be used for effect, putting emphasis on the greeting, e.g.:
Hello, Old Pedant.
"Hello" is said first, then a pause, then the name. Normally, people would say:
Hello Old Pedant.
Without any pauses in their speech.

__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-12-2013 at 10:20 PM..
Philip M is offline   Reply With Quote
Old 02-12-2013, 10:27 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahh...British usage is more flexible.

*THE* standard for American style absolutely requires the comma.

(Of course, it requires other very silly things. My favorite:
Quote:
Press the key labelled "ENTER," and then wait ten seconds.
See the placement of the comma there? How many people really have a key labelled ENTER, (that is, with the comma on the key)??

None, of course. But the style guide absolutely forbids the sequence ", (QUOTE then COMMA).

When I was writing a programming book,the copy editor was constantly going and putting commas in before quote marks like that, even though they obviously destroyed the meaning of the text. So I was constantly rewriting my sentences to ensure no comma would be needed.

And you know where this came from? From the days of the LINOTYPE machines! Where you saved a half-pica by putting the comma first.

*SIGH* I learned later that you British are much more sane on this issue.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-13-2013, 08:20 AM   PM User | #10
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
In the UK a comma or full stop before a quotation mark would be regarded as incorrect. He said "Hello Old Pedant". I know that US practice is different.

Quote:
Originally Posted by Old Pedant View Post
*SIGH* I learned later that you British are much more sane on this issue.
Yes, that is true. Same with other topics such as the rejection of things like creationism and assault rifles in schools ......... Also, we don't think that our Queen was not born in the UK. And we do not elect as our leaders politicians who just have the most money.

At the risk of an infraction for off-topic, I am amazed that people in the USA are sometimes as barmy as the ayatollahs in Tehran.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-13-2013 at 08:24 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, javascript

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 06:03 PM.


Advertisement
Log in to turn off these ads.