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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Location
    NYC
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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
    Last edited by Philip M; 02-12-2013 at 09:07 PM.

    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.

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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>
    Last edited by Philip M; 02-12-2013 at 09:39 PM.

    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.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    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?
    Last edited by Old Pedant; 02-12-2013 at 10:05 PM.
    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

    Last edited by Philip M; 02-12-2013 at 10:20 PM.

    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    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:
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.
    Last edited by Philip M; 02-13-2013 at 08:24 AM.

    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.


  •  

    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
    •