Reload individual input with JS

I'd like to refresh an input field every second while leaving the rest of the page untouched. Is there a way to do this with Javascript? I'm a little unsure how to write it, but I think it would look like..

var myVar;

function autoRefresh() {
  myVar = setInterval(loadValue, 1000);
}

function loadValue() {
  input.reload("reload").value
}
<input type="text" id="reload">

I'm sure the syntax is wrong, but any input would be great!

Upvotes: 0

Views: 1526

Answers (2)

fyasir
fyasir

Reputation: 2970

var myVar;
var counter = 1;
var input = document.getElementById('reload');

function autoRefresh() {
  myVar = setInterval(loadValue, 1000);
}

function loadValue() {
  input.value = counter++;
}

autoRefresh();
<input type="text" id="reload">

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65835

You're on the right track, but what should the field's value be when it is refreshed?

In this example, I'm turning the field into a clock by refreshing its value with the current time every second:

document.getElementById("loadTime").textContent = new Date().toLocaleTimeString();

var input = document.getElementById("reload");

setInterval(function(){
  input.value = new Date().toLocaleTimeString();
}, 1000);
<div>Time page was loaded: <span id="loadTime"></span></div>
Current Time:<input type="text" id="reload">

Upvotes: 1

Related Questions