Home > Blockchain >  How to get and set time input value?
How to get and set time input value?

Time:10-21

I have an input with type "time".

<input type="time" id="time1" value="18:00">


Now, if I try to get the default value via console. I'm getting :

console.log(document.getElementById("#time1").value())
VM250:1 Uncaught TypeError: Cannot read properties of null (reading 'value')
    at <anonymous>:1:47

Can you give me some suggestions how to set the value of the time input, if I'm getting a string value from an object.

For example: "06:00 AM"

I know that it will not accept that string since there's a unexpected character in it.

CodePudding user response:

Instead of using value(), use value

console.log(document.getElementById("time1").value);
<input type="time" name="car" id="time1" value="18:00">
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

There are 2 errors in your code.

1 - No need to use # when you use getElementById. Because we are already telling browser to get by Id. However you would need # if you are using querySelector.

  1. value is not a function.

So the code should be

console.log(document.getElementById("time1").value)

Reference: https://www.w3schools.com/jsref/prop_text_value.asp

CodePudding user response:

Dont use the # on the document.getElementById.

Use either

document.getElementById("time1");
//-- or --
document.querySelector("#time1");
  • Related