Home » Javascript » Javascript to get start and end time based on radio button

Javascript to get start and end time based on radio button

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have an HTML code that looks like below.

<dd id="shifts">
<pre id="pre">
<input type="radio" name="shifts[]" id="shiftsValue" class="shiftsValue" onclick="shiftCheck();" value="185142249"> Shift No.1      Start time : 06:30:00 AM      End time : 10:30:00 AM<br>
</pre>
<pre id="pre"><input type="radio" name="shifts[]" id="shiftsValue" class="shiftsValue" onclick="shiftCheck();" value="185142250"> Shift No.2      Start time : 11:30:00 AM      End time : 15:30:00 PM<br>
</pre>
</dd>

This has worked fine and now there is a need to do time validation on time input type. I need to check the input for time such that the time entered is between the start and end time.

My thought is to get the string from the checked radio button and then extract the start and end time from the string. When the input is made into time element then check if it is between the start and end time.

I am having difficulty in extracting the string from above.

Answers:

I think I got it working

$('input[type=radio][class=shiftsValue]:checked').closest('pre').text()

seems to be getting the string for the <pre> of the selected radio button. Is this correct or should I try something else?