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

spen123
  • 3,464
  • 11
  • 39
  • 52

2 Answers2

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;
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • So 1, and 3 are done I should of included them in mode code sorry, and what do I do number 2 – spen123 Dec 03 '15 at 05:51
  • 1
    http://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. – potatopeelings Dec 03 '15 at 05:56
  • 1
    i.e. just do `input_open.value = h + ":" + m;` – potatopeelings Dec 03 '15 at 05:56
  • 1
    So you answer the question and mark it as duplicate too? lol – Rahul Desai Dec 03 '15 at 06:38
  • :-) 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. – potatopeelings Dec 03 '15 at 06:58
-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);
Arg0n
  • 8,283
  • 2
  • 21
  • 38