Ali Fakhry
Ali Fakhry

Reputation: 25

Returning Both Hours and Minutes for User Input

How would I return the hours and minutes for a JavaScript time input?

Here is the HTML

<input type="time" id="alarm" name="alarm"
   min="00:00" max="24:00" required>

And then here is the javascript

checkSaveAlarm.addEventListener("click", () => {
  alarmMinutes = alarmBox.value.getMinutes();
  alarmHours = alarmBox.value.getHours();
  alarmFunction(alarmHours, alarmMinutes); });

Doing the above returns an error.

Upvotes: 0

Views: 24

Answers (1)

bel3atar
bel3atar

Reputation: 943

const alarmFunction = console.log
document.querySelector('input').addEventListener("change", ({ target: { value } }) => {
  const [alarmHours, alarmMinutes] = value.split(':');
  alarmFunction(alarmHours, alarmMinutes);
});
<input type="time" id="alarm" name="alarm" min="00:00" max="24:00" required>

Upvotes: 1

Related Questions