...

View Full Version : Inserting JS value in form



nickyfraggle
01-21-2009, 11:15 AM
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:



<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:



<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

abduraooft
01-21-2009, 11:33 AM
<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>

<input type="hidden" name="date" id="date" />

nickyfraggle
01-21-2009, 11:40 AM
Thanks! I knew it was going to be simple...the brains on a go slow this morning!

Thanks again,

Nicky

pradeep19java
11-27-2009, 04:35 AM
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>
-------------------------------------------------------------------------

bdl
11-27-2009, 05:03 AM
pradeep19java> Please start your own thread, don't hijack someone else's thread from almost a year ago.

Philip M
11-27-2009, 07:31 AM
And please help us to help you by following the posting guidelines and wrapping your code in
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum