...

View Full Version : Time in textbox



toddn
01-03-2008, 10:31 PM
Hey something really simple that I just cant figure out.

I want to insert the time into a textbox how do I do that, I'm just learning javascript so I'm not sure of how to set it up!!

Thanks:eek:


Oh and also I have two checkboxes in my form and if one is checked i want to make it so the other cannot be checked. How would I do that without naming the two checkboxes the same. I cant use radio buttons because the program it is interfacing with does not have radio buttons.

Thanks again

gencoglu4
01-03-2008, 10:48 PM
Try this:



<html>
<head>
<script type="text/javascript">
function TickTick()
{
var today=new Date()
var dd = today.getDate();
var mm = today.getMonth();
var yy = today.getFullYear();
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
m=secTicker(m)
s=secTicker(s)

document.getElementById('Clock').value="Time="+h+":"+m+":"+s;
t=setTimeout('TickTick()',1000)

}

function secTicker(i)// This Function is to add zero before minutes and second value which is less than 10
{
if (i < 10){i = "0" + i}return i; }
</script>
</head>

<body onload="TickTick()">

<input type="text" id="Clock"name="Clock">
</body>
</html>

toddn
01-03-2008, 11:03 PM
Cool it works except for I need a 12 hour format not a 24 hour format

I appreciate your help and anymore help that is given on this subject

toddn
01-03-2008, 11:07 PM
If I try to erase "Time="
document.getElementById('Clock').value="Time="+h+":"+m+":"+s;

or take out the parentheses it doesnt work, I'm not quite understanding why this is isnt "time" the object its calling??

gencoglu4
01-03-2008, 11:31 PM
I erased "Time="..see if it works ?




<script type="text/javascript">
function TickTick()
{
var today=new Date()

var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
m=secTicker(m)
s=secTicker(s)

document.getElementById('Clock').value=h+":"+m+":"+s;
t=setTimeout('TickTick()',1000)

}
function secTicker(i)
{
if (i < 10){i = "0" + i}return i; }

</script>
</head>

<body onload="TickTick()">

<input type="text" id="Clock"name="Clock">

toddn
01-03-2008, 11:34 PM
I thought i did that i mustve left the " before h+, thanks for that. If i wanted to get it to show say 2:34 pm instead of 14:34 what part of the code am i supposed to be looking at??

I guess I dont understand where it is getting the time from in that format, can someone also explain it??

arilia
01-04-2008, 08:48 AM
Remember that the time you are getting is the client-side time, i.e. the time of the user PC.

Maybe the clock of the client may be wrong, or it may be set on another time-zone.

Think about using server side scripting (such as PHP) to retrieve the time

Philip M
01-04-2008, 09:10 AM
I thought i did that i mustve left the " before h+, thanks for that. If i wanted to get it to show say 2:34 pm instead of 14:34 what part of the code am i supposed to be looking at??

I guess I dont understand where it is getting the time from in that format, can someone also explain it??

<script type="text/javascript">
function TickTick() {
var today=new Date();;

var h=today.getHours();
var h1 = h;
var m=today.getMinutes();
var s=today.getSeconds();
var ampm = " AM";

if (h >= 12) {ampm = " PM"}
if (h >= 13) {h = h - 12}
if (h1 == 0) {h = 12} // i.e. 12.xx am

h = secTicker(h); // if zero before hours required
m = secTicker(m);
s = secTicker(s);

document.getElementById('Clock').value = h + ":" + m + ":"+ s + ampm;
t=setTimeout('TickTick()',1000);
}

function secTicker(i) {
if (i < 10) {i = "0" + i}
return i;
}

</script>
</head>

<body onload = "TickTick()">
<input type = "text" id = "Clock" name = "Clock">

Your second question:-
The whole point of checkboxes is that more than one can be checked, while only one radio of a set may be selected. Another way is to use options. But if you insist:-

<form name = "myform" onsubmit = "return checkChecks()">
<input type = "checkbox" name = "chk1" value = "First">FIRST<br>
<input type = "checkbox" name = "chk2" value = "Second">SECOND<br><br>
<input type = "submit" value = "SUBMIT">
</form>

<script type = "text/javascript">

function checkChecks() {
if ((document.myform.chk1.checked) && (document.myform.chk2.checked)) {
alert ("You may only check ONE checkbox");
return false;
}
}

</script>

toddn
01-04-2008, 08:38 PM
unfortunately that wasnt quite what i was looking for, i think i'm looking more for a boolean function, if a = true then b = false i just dont know how to code it

<tr><td class=m>Check In</td>
<td class=m><input type=checkbox size=40 name=_fid_28 class=m> Check Out</td>
<td class=m><input type=checkbox size=40 name=_fid_29 ></td>


this is my code

toddn
01-04-2008, 11:16 PM
basically i need it to act like a radio button, if one is checked then the other unchecks

Philip M
01-05-2008, 09:59 AM
<form name = "myform" >
<input type = "checkbox" name = "chk1" value = "First" onclick= "checkChecks1()">FIRST<br>
<input type = "checkbox" name = "chk2" value = "Second" onclick = "checkChecks2()">SECOND<br><br>
</form>

<script type = "text/javascript">

function checkChecks1() {
if (document.myform.chk2.checked) {
document.myform.chk2.checked = false;
alert ("You may only check ONE checkbox"); /// delete as required
return false;
}
}
function checkChecks2() {
if (document.myform.chk1.checked) {
document.myform.chk1.checked = false;
alert ("You may only check ONE checkbox"); // delete as required
return false;
}
}
</script>

It is your responsibility to die() if necessary….. - PHP Manual



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum