0

I have a little issue with a webform. I am setting a booking system and some items are only available on some days of the week. I have modified the date field to be a textbox with popup calendar I need to show/ hide some field based on the Day of The Week selected ( if Day is Tuesday or Day is Friday then Show the fields, otherwise, hid them) Any help is welcome, because with the conditionals, i can only do it based on date not on the day itself.

thank you

0

I think the easiest solution is to do this with jQuery.

To get the value from the datepicker field and convert it to a weekday you might do something like this.

Add this function to your JS file:

// Accepts a Date object or date string that is recognized by the Date.parse() method
function getDayOfWeek(date) {
  var dayOfWeek = new Date(date).getDay();    
  return isNaN(dayOfWeek) ? null : [0, 1, 2, 3, 4, 5, 6][dayOfWeek];
}

Use this code to get the value from the datepicker:

var datepicker = $('#your_datepicker_id').datepicker("getDate");

Then do like this:

var today = new Date();
if (!today.getDay() == getDayOfWeek(datepicker)) {
  $('.field-names-to-hide').hide();
}

I think that something like this will do it.

11
  • Thank you for the tip. Is it possible in webforms to add the code inline instead of in a separate js file ? – Chérif Kagni May 5 '18 at 12:06
  • Yes, you can add a markup field, set text format as "PHP", and add your JS code using drupal_add_js(). – Kirill Shabunov May 5 '18 at 12:51
  • thank you. But I would like to get the Day selected in the DatePicker, not the Today's day so the display should be conditioned to the selected weekday. Sorry, I am more into C#, just beginning Js for my own website. – Chérif Kagni May 5 '18 at 13:49
  • Try to take a look here stackoverflow.com/questions/4919873/… – user72672 May 5 '18 at 14:05
  • Thank you again.but it is only working for the first week. Any hint on that ? – Chérif Kagni May 5 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.