Change javascript/php function from form to div

Asked

Viewed 68 times

1

How do I change the function below? I want to display the time in a div.

 <HTML>
 <HEAD>
 <TITLE>cronometro</TITLE>

 <script language="JavaScript">
 <!--
 function showtime()
 { setTimeout("showtime();",1000);
 callerdate.setTime(callerdate.getTime()+1000);
 var hh = String(callerdate.getHours());
 var mm = String(callerdate.getMinutes());
 var ss = String(callerdate.getSeconds());
 document.clock.face.value =
 ((hh < 10) ? " " : "") + hh +
 ((mm < 10) ? ":0" : ":") + mm +
 ((ss < 10) ? ":0" : ":") + ss;

 }
 callerdate = new Date(<?php 

date_default_timezone_set('America/sao_paulo');
$brasil = date('Y,m,d,H,i,s', time());

echo $brasil;

?>);
//-->
</script>
</HEAD>
<meta name="" content="">
<body onLoad="showtime()">
<form  name="clock"><input name="face" value=""></input>
</form> 
</body>
</HTML> 
  • But what’s wrong, the function or just not being able to display the time in a div? Your html is all wrong tbm, has two body tags and the input tag has no closing tag.

  • Good morning Leandrade, despite the bugs that have already been fixed is all working normal, I just want to remove the form and input to put the information in a div.

  • But then it is not only delete the form and the input and in place insert the div and the same way that called the function in the tag body, call in the created div?

  • but I need to pass values to div(name="clock", name="face", value=" ") as I do?

1 answer

3


For this, you need to change basically 3 lines. The first is the line

 document.clock.face.value =

To be amended to:

document.getElementById("clock").innerHTML = 

Then change the lines in the HTML part:

<form  name="clock"><input name="face" value=""></input>
</form> 

To:

<div id="clock"></div> 

In general, your code will look like this:

 <HTML>
 <HEAD>
 <TITLE>cronometro</TITLE>

 <script language="JavaScript">
 <!--
 function showtime()
 { setTimeout("showtime();",1000);
 callerdate.setTime(callerdate.getTime()+1000);
 var hh = String(callerdate.getHours());
 var mm = String(callerdate.getMinutes());
 var ss = String(callerdate.getSeconds());
 document.getElementById("clock").innerHTML = 
 ((hh < 10) ? " " : "") + hh +
 ((mm < 10) ? ":0" : ":") + mm +
 ((ss < 10) ? ":0" : ":") + ss;

 }
 callerdate = new Date(<?php 

date_default_timezone_set('America/sao_paulo');
$brasil = date('Y,m,d,H,i,s', time());

echo $brasil;

?>);
//-->
</script>
</HEAD>
<meta name="" content="">
<body onLoad="showtime()">
<div id="clock"></div> 
</body>
</HTML> 
  • Thank you David, at first it worked out any doubt I come back here.

  • Oops. For nothing :) Quiet

  • Good afternoon David, sorry to bother you but I noticed that after 30 minutes/ 1 hour the clock starts late you tell me why?

  • At first I don’t know. But would it be late compared to which clock? That of your own computer? Or an external clock?

  • computer, if you put this code to run on wampserver and leave the browser open with it running you will see a delay in seconds.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.