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 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to display days , hours and minutes in my page which is comming from java script

    hi
    i got little problem. can you give me suggestion.
    Code:
    <script type = "text/javascript">
    var hours = document.getElementById("totalTime").value;
    var days = Math.floor(hours/24);
    var hrs = Math.floor(hours%24);
    var decimal = hours - Math.floor(hours);
    var minutes = Math.round(decimal * 60);
    
    document.getElementById("actualDays").value= +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";
    
    </script>
    based on above script i am able to display my values in text box like below.
    Code:
    <input  name="actualDays"  id="actualDays" value="" size="30"  readonly="yes"/>
    but i need to display these values as a text values (using lable or span....).
    can you help me for this

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Use innerHTML.

    Code:
    document.getElementById("actualDays").innerHTML = ....

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Replay

    Thank you. i users inner.html but i got unknown run time error.
    i hope you understand my problem: i am trying to display values which is coming java script


    Thank you,
    vamsidar k

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by kanna443 View Post
    i users inner.html but i got unknown run time error.
    Did you spell innerHTML (not inner.HTML) correctly? Javascript is case sensitive.

    As it stands your script will run as soon as the page loads, and before the user has entered the required data. You need to wrap the script within a function.

    Code:
    Enter time in hours <input type = "text" id = "TotalTime" onblur = "showhm()">
    <span id = "actualDays"></span>
    
    <script type = "text/javascript">
    
    function showhm() {
    
    var hours = document.getElementById("totalTime").value;
    var days = Math.floor(hours/24);
    var hrs = Math.floor(hours%24);
    var decimal = hours - Math.floor(hours);
    var minutes = Math.round(decimal * 60);
    
    document.getElementById("actualDays").innerHTML= +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";
    
    }
    </script>
    But you need to validate that the value entered by the user is a number. The simplest way is

    var hours = Number(document.getElementById("totalTime").value) || 0;
    Last edited by Philip M; 03-20-2013 at 08:54 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.

  • #5
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you.but i am using hidden text field.
    Code:
     <input type="hidden" name="totalTime"  id="totalTime" value="#getTime.total#">
    "totalTime" value i am getting from database using coldfusion code(server side technology). so i am passing this "totaltime" value to java script and getting days, hours , minutes and i need to display this as text . can you give me suggestion to display as text

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    can you give me suggestion to display as text
    Yes - I did that in post#4. What more do you want?

    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.

  • #7
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    replay

    Code:
    Thank you Philip...i will elaborate my requirement and my concern.so that you can easily understand
    my question is, i am not going to enter the total time in text box. the value of "totaltime" getting from database. so just i need to display after convert to Days ,hours, minutes.

    so that i have taken the hidden text filed for getting the "totaltime" and i am passing these value to java script like below.
    Code:
    var hours = document.getElementById("totalTime").value;
    thank you

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile working fine

    Thank you so much. its working.

    i am very happy.

    using innerHTML i resolved my issue

    Regards
    kanna


  •  

    Posting Permissions

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