I am trying to get the time selected from SharePoint list and store it one variable. I have attached an image for reference, Can anyone help me with this?
2 Answers
No need to use the jquery, you can do that using vanilla JS querySelector
var time = document.querySelector("select[id='Scheduled_x0020_time_0c63521b-1292-45ae-a881-f85e14c83ca7_$DateTimeFieldDateHours'").value + ":" + document.querySelector("select[id='Scheduled_x0020_time_0c63521b-1292-45ae-a881-f85e14c83ca7_$DateTimeFieldDateMinutes'").value
console.log(time);
Explanation,
The datetime field has two select controls to show the time value
DateTimeFieldDateHoursfor HoursDateTimeFieldDateMinutesfor Minutes
So you are trying to query the selector with the actual ID id=, or using contains id=^ then try to concatenate both values to get the desired value as shown below.
Output
-
Thank you for the answer, will try this and let you know.Prashanth Edige– Prashanth Edige2018-09-13 13:21:17 +00:00Commented Sep 13, 2018 at 13:21
Mohamed's code is right, if you want to use jQuery code, check the code below.
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function(){
var fieldName="Select Scheduled Departure Time";
var fieldDateHours=$(".ms-standardheader:contains('"+fieldName+"')").closest("tr").find("select[id$='_$DateTimeFieldDateHours']").val();
var fieldDateMinutes=$(".ms-standardheader:contains('"+fieldName+"')").closest("tr").find("select[id$='_$DateTimeFieldDateMinutes']").val();
console.log(fieldDateHours+":"+fieldDateMinutes);
})
</script>
-
Thank you for the answer, will try this and let you know if its working.Prashanth Edige– Prashanth Edige2018-09-13 13:21:36 +00:00Commented Sep 13, 2018 at 13:21
-
This function does not get triggered , I want to use this function in the change.function(). Can I use this there?Prashanth Edige– Prashanth Edige2018-09-13 19:10:42 +00:00Commented Sep 13, 2018 at 19:10
-
It says Uncaught TypeError: $(...).closest is not a function. when I try to run the code which you have mentioned.Prashanth Edige– Prashanth Edige2018-09-19 20:28:51 +00:00Commented Sep 19, 2018 at 20:28
-
My api request accepts 24 hour format date and I have 12 hour format date like this 9:00 PM and I need it to be as 21:00. How can I do that here in jquery?Prashanth Edige– Prashanth Edige2018-09-19 20:34:52 +00:00Commented Sep 19, 2018 at 20:34
