0

Need the output of the time script to change the input of a input field but can't figure it out.


<script>
function addZero(i) {
if (i < 10) {
    i = "0" + i;
}
return i;
}

function checkouttimeFunction() {
var d = new Date();
var x = document.getElementById("demo");
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
x.innerHTML = h + ":" + m + ":" + s;
var elem = document.getElementById("cout");
elem.value = document.getElementById("demo");
}
</script>
1
  • 1
    Are the functions actually called from another script or from the html? Commented Jan 20, 2015 at 7:29

3 Answers 3

1

Looks like on the last line in checkouttimeFunction() you want do to:

elem.value = x.innerHTML;

Other than:

elem.value = document.getElementById("demo");

As you are trying to set the value to be a DOM element.

Fiddle Example

Sign up to request clarification or add additional context in comments.

Comments

0

I'm assuming you want the innerHTML text of x to be displayed in the input field elem.

You cant set the 'value' attribute to an HTML element, but you can instead just set it to the desired text (e.g the time).

<script>
function addZero(i) {
if (i < 10) {
    i = "0" + i;
}
return i;
}

function checkouttimeFunction() {
var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
var elem = document.getElementById("cout");
elem.value = h + ":" + m + ":" + s;
}
</script>

1 Comment

Yes value is right for sure. But you cant set a element as value.
0

You should change

elem.value = document.getElementById("demo");

to

elem.value = document.getElementById("demo").innerHTML;

That should give you what you need. Because you need the content of the element and document.getElementById() will just give you the element, Which will be in the form of an Object.

Hope this helps.

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.