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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Inserting JS value in form

    Hello,

    I know very little Javascript, so the answer to this is probably VERY simple :-)

    I have some javascript working out the current date for me. I want to output this date into the value of an input field in a form.

    My javascript to output the date is:

    Code:
    <script type="text/javascript">
                <!--
                var currentTime = new Date()
                var month = currentTime.getMonth() + 1
                var day = currentTime.getDate()
                var year = currentTime.getFullYear()
                document.write(month + "/" + day + "/" + year)
                //-->
    </script>
    And I'd like this to output in the value of the input field below:

    Code:
    <input type="hidden" name="date" value="js date to go here"/>
    I've tried using script tags and document.write, but it messes up my HTML and I'm not sure what exactly it is that I'm doing wrong!!

    Sorry for the silly question...

    Thanks for your help

    Nicky

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    <script type="text/javascript">
    window.onload=function(){
                var currentTime = new Date()
                var month = currentTime.getMonth() + 1
                var day = currentTime.getDate()
                var year = currentTime.getFullYear()
               // document.write(month + "/" + day + "/" + year)
             document.getElementById('date').value=month + "/" + day + "/" + year;
    }           
    </script>
    Code:
    <input type="hidden" name="date" id="date" />
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    nickyfraggle (01-21-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks! I knew it was going to be simple...the brains on a go slow this morning!

    Thanks again,

    Nicky

  • #4
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I'm unable to pass script result to a form. Please find below my code and correct me / suggest me.

    -------------------------------------------------------------------------
    <html>
    <head>
    <title>Selected Radio Buttons</title>
    </head>

    <body>
    <script language="JavaScript">
    function get_radio_value()
    {
    var j = 0;
    var radio_buttons = new Array();
    var the_form = window.document.forms[0];
    for(var i=0; i<the_form.length; i++)
    {
    var temp = the_form.elements[i].type;
    if((temp == "radio") && (the_form.elements[i].checked)) { radio_buttons[j] = the_form.elements[i].value; j++; }
    }
    for(var k=0; k<radio_buttons.length; k++)
    {
    if (radio_buttons[k].toString()=="A")
    document.getElementById('result').value=radio_buttons[k];

    }
    }
    </script>


    <form name="testform">
    <input type="radio" name="r_1" value="A">A<br>
    <input type="radio" name="r_1" value="B">B<br>

    <input type="submit" value="SELECT" onclick="get_radio_value()"/>
    <br><br>
    <input type="text" name="result" id="result"/>
    </form>
    </body>
    </html>
    -------------------------------------------------------------------------

  • #5
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    pradeep19java> Please start your own thread, don't hijack someone else's thread from almost a year ago.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    And please help us to help you by following the posting guidelines and wrapping your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [ code ] and closing [ /code ] tags - omit the spaces.

    Your code is absurdly complicated. Here you are - but please follow the posting guidelines in future, and refrain from reviving ancient threads.

    Code:
    <script type = "text/javascript">
    function get_radio_value () {
    var chosen = "None";
    var len = document.testform.r_1.length;
    for (i = 0; i <len; i++) {
    if (document.testform.r_1[i].checked) {
    chosen = document.testform.r_1[i].value;
    }
    }
    document.testform.result.value = chosen;
    }
    
    </script>
    
    
    <form name="testform">
    <input type="radio" name="r_1" value="A">A<br>
    <input type="radio" name="r_1" value="B">B<br>
    
    <input type="button" value="SELECT" onclick="get_radio_value()"/>
    <br><br>
    <input type="text" name="result" id="result" readonly/>
    </form>
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


    Quizmaster: In Shakespeare's Hamlet, a famous quotation is "Get thee to a ......" what?
    Contestant: Church on time.
    Last edited by Philip M; 11-27-2009 at 08:59 AM.


  •  

    Posting Permissions

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