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.
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".
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.
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".
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.
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".
(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
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.
*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.
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
*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.