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

    Java in HTML Text Box

    Can someone show me how to write a form input text box html code that will display the results of this script in the html text box? Thank you.

    [CODE]<script type="text/javascript">
    <!--
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    document.write("<b>" + month + "/" + day + "/" + year + "&nbsp;" + "</b>")

    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes

    var suffix = "AM";
    if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
    }
    if (hours == 0) {
    hours = 12;
    }
    document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
    //-->
    </script>[CODE]

  • #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 to use separate variables (currentDate/currentTime) for time and date. Just set rightNow = new Date() and get everything off of that.

    document.write is not a good idea. If it is run after a page loads, it overwrites the whole page (including the javascript that generated it.) Use modern day DOM methods (create a div, give it an id, and set the innerHTML of that id to what you want.)

    If you need to set the value of a text input, give the input an id and use document.getElementById(' input name ').value = "foo";
    ^_^

    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
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you Wolfshade for your help but I am a JavaScript newbie and what you said is mostly Greek to me. Can you show me what that would look like with the changes you described. I would be very grateful. Thank you.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Modernising the code slightly to get rid of the HTML comment to hide the script from Internet Explorer 2 and wrapping it inside a function to make it unobtrusive and to define all the local variables at the top in one go - you end up with the following:

    Code:
    <b id="dt"></b><b id="tm"><b>
    <script type="text/javascript">
    (function() {"use strict";
    var currentDate, day, month, year, hours, minutes, suffix;
    currentDate = new Date();
    day = currentDate.getDate();
    month = currentDate.getMonth() + 1;
    year = currentDate.getFullYear();
    document.getElementById('dt').innerHTML = month + "/" + day + "/" + year;
    
    hours = currentDate.getHours();
    minutes = currentDate.getMinutes();
    
     if (minutes < 10)
     minutes = "0" + minutes;
    
    suffix = "AM";
     if (hours >= 12) {
     suffix = "PM";
     hours = hours - 12;
     }
     if (hours == 0) {
     hours = 12;
     }
     document.getElementById('tm').innerHTML = hours + ":" + minutes + " " + suffix ;
    })();
     </script>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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