3

I want to show current time within input field. I have used id local-time. But in the input field, current time is not showing.

Current Time : <input type="text" id = "local-time" value= ""/>
<script>
setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
pad(local.getSeconds());

$('#local-time').html(localdatetime);
}, 1000);

function pad(t) {
var st = "" + t;

while (st.length < 2)
st = "0" + st;

return st;  
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

3 Answers 3

2

Simply Edit the script: change html to val

<script>
setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
pad(local.getSeconds());

$('#local-time').val(localdatetime);
}, 1000);

function pad(t) {
var st = "" + t;

while (st.length < 2)
st = "0" + st;

return st;  
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You tried to set the html instead of value of that input element.

Have a look at this for the uses of .val() .html() .text()

$('#local-time').val(localdatetime);

Comments

1
setInterval(function() {
  var local = new Date();
  var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
  pad(local.getSeconds());

  $('#local-time').val(localdatetime);
}, 1000);

function pad(t) {
   var st = "" + t;

   while (st.length < 2)
   st = "0" + st;

   return st;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.