7

I have a dialog box that has an HTML5 input field with a type="time" in it.

When the dialog appears, I want to default the editing to the "minutes" portion of the dialog. I can't see a way to do it.

Here is a jsFiddle example that tries to use:

$("#t")[0].setSelectionRange(3, 5);

to no avail.

Is there a way to do this?


NOTE: the w3c spec here specifically has this quote:

The following IDL attributes and methods do not apply to the element: checked, files, selectionStart, selectionEnd, and selectionDirection IDL attributes; select(), setRangeText(), and setSelectionRange() methods.

Which might mean this simply isn't possible.


NOTE 2: Chrome throws this error when attempting to call this method. Other browsers may throw different errors:

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
CleverPatrick
  • 9,261
  • 5
  • 63
  • 86
  • 1
    I believe your current code will give an exception of `"Object [object Object] has no method 'setSelectionRange'"`, this is because the JQuery object doesn't have that method. You need to call it on a DOM element, you can do this with `$("#t")[0]` or plain javascript `getElementById`... however, it seems that will then throw a new error: `"An attempt was made to use an object that is not, or is no longer, usable."`, and I am not sure how to fix that one. Note, if you use a normal `Text` input type then it would work ok, so seems specific to `Time` input type – musefan Jan 13 '14 at 16:38
  • 1
    Hey thanks. Fixed my code. That was just a typo when typing up the example. I was really concerned about the "An attempt was made..." issue. Added more notes to that affect in the description. – CleverPatrick Jan 13 '14 at 17:48
  • "Which might mean this simply isn't possible." I think that's the answer unfortunately :( – Daniel Tonon Nov 01 '21 at 07:59

1 Answers1

0

Just set it using some js.

document.getElementsByTagName("INPUT")[0].value = "13:24"
<input type="time" id="appt" name="appt" min="09:00" max="18:00" required>
Endothermic_Dragon
  • 1,147
  • 3
  • 13