0

I am trying to set the value of my time input with javascript here is what I am doing

var div = document.createElement("DIV");
var div_id = "div_hours_" + "1";

var input_open = document.createElement("INPUT");
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
input_open.type = 'time';
input_open.name = 'time_open_input';
input_open.value = h+ ":" + m + " am";

div.appendChild(input_open);
el("body_div").appendChild(div);

But when I do this, I see the time input, but its value is empty?

Why isn't the value being input?

Thanks for the help

0

2 Answers 2

0

You need to

  1. Define the variable numOfHours
  2. Remove the line input_open.type = 'time'; (time is not yet supported in IE)
  3. Add a function for el (or you could replace it with the line - document.getElementById("body_div").appendChild(div) assuming your are looking to append to a div with id as body_div

If you are using Chrome just change your last line to (instead of doing 2. above)

input_open.value = h + ":" + m;
Sign up to request clarification or add additional context in comments.

5 Comments

So 1, and 3 are done I should of included them in mode code sorry, and what do I do number 2
caniuse.com/#feat=input-datetime - it's not supported yet in IE. If you are using Chrome just lose the " am" in your last line and it should work.
i.e. just do input_open.value = h + ":" + m;
So you answer the question and mark it as duplicate too? lol
:-) it "seemed" like there were multiple problems at first, but then OP said that they were just a result of formatting the code for the question. And then I came back in a while, saw a comment (against the question I think) and went - yeah that's right, its a dupe.
-1

Reference

value = time

A string representing a time (with no timezone information).

A valid partial-time as defined in [RFC 3339].

Examples: 23:20:50.52 17:39:57

You may want to change the type to plain text.

See this JSFiddle

JavaScript

var div = document.createElement("div");
var div_id = "div_hours_" + "1";

var input_open = document.createElement("input");
var today=new Date();
var h=today.getHours() < 10 ? "0" + today.getHours() : today.getHours();
var m=today.getMinutes() < 10 ? "0" + today.getMinutes() : today.getMinutes();
var s=today.getSeconds() < 10 ? "0" + today.getSeconds() : today.getSeconds();
input_open.type = 'text';
input_open.name = 'time_open_input';
input_open.value = h + ":" + m + " am";

div.appendChild(input_open);
document.body.appendChild(div);

2 Comments

This is more suited to a comment
Wow, that's a pretty harsh reaction, happy coding

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.