1

I have an input element of type time on my page which I save off the user time selection in the format which was given 12:22. But I am having trouble loading the time 12:22 string back into the element on a return session.

How does one load the time, and show the time, in a type input element?


Element

<input type="time" id="Time1"/>

I've tried

$('#Time1').val = '3:22';

and

$('#Time1').value = '3:22';

with no luck.


My initial value in the config file (not set by the user) had a value of 3 and should be 03. I am marking the answers related to the first problem of assignment and not the loading value which has to be XX not X.

2
  • 1
    Try $('#Time1').val('12:22'); or $('#Time1')[0].value = '12:22'; Commented Jun 25, 2021 at 3:19
  • I tried the hardcoded first you suggested, and got the time. Let me investigate the transport json with the time to see why it may be failing. Commented Jun 25, 2021 at 3:22

2 Answers 2

1

The value property does not exist in a jQuery object. Use the val() method:

$('#Time1').val('12:22');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="time" id="Time1"/>

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

1 Comment

This resolved my initial problem which solved my issue and I am marking this as such. thx. The other post divined by second problem of trying to load 3:45 and not 03:45 which I didn't properly show initially....
1

you can do this by the below code.

function AssignTime() {
   
   var d = new Date();
  document.getElementById("Time1").value = d.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'}).split(" ")[0];//time should be in 24 hrs format.
}
Time: <input type="time" id="Time1">
<button onclick="AssignTime()">Assign</button>

2 Comments

This post divined my second problem ("time should be in 24 hrs format. }" of trying to load 3:45 and not 03:45 which I didn't properly show initially (my bad)....I would like to mark this as the answer, but went with the other one.
@ΩmegaMan no problem bro. you can mark if u want :)

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.