You are viewing limited content. For full access, please sign in.

Question

Question

Confused as to how to set date defaults for date fields by class name

asked on June 2, 2021 Show version history

I found reference #1 (below) of how to set defaults for a date field picker. For some reason you have to set the default of beforeShow to a function which then sets the option of beforeShowDay.

Not sure what the redundancy is here, but it does work.

So I came up with this code:

  function checkField(input) {
   
     $(input).datepicker("option", {beforeShowDay: function(d){ 
            
      var ds = d.toDateString();
      
      return [ds==getPreviousMonday() || ds==getFutureMondayOf(1) || ds==getFutureMondayOf(2) || ds==getFutureMondayOf(3) || ds==getFutureMondayOf(4) ,""];}
   });
    
  }
  
  $.datepicker.setDefaults({beforeShow: checkField});

It works! The problem with this code is that $.datepicker is not class dependent, which means it will seek out and apply to any date field it finds. That is not going to work in many cases. So I found reference #2 (below) where it was stated you need to use an option instead of setDefaults if you want to work by class name.

So this is odd, now I am switching back to the option method to call the beforeShow so I can call the beforeShowDay

I switched the last line of my code to this:

 $('.myDateField').datepicker("option", {beforeShow: checkField});

Nothing happens.

I tried just setting my beforeShowDay this way as well instead of creating a function called checkField, but nothing happens there either.

I exclusively use classes to make sure I am working with the right field(s). How would I set defaults on a date by class name?

Reference #1 https://answers.laserfiche.com/questions/173587/Restricting-Date-Picker#174123

Reference #2 https://answers.laserfiche.com/questions/182596/Forms-Calendar--Restrict-to-specific-list-of-dates#182648

0 0

Answer

SELECTED ANSWER
replied on June 2, 2021 Show version history

Hi Chad,

Your direction is correct. In 2nd post, the working code find the input element of date field with CSS class.

 

Try

$('.myDateField input').datepicker("option", {beforeShow: checkField});

 

0 0
replied on June 3, 2021

Thank you! I was just missing the input part!

So it was correct that I must call a function with beforeShow that then calls a function for beforeShowDay.

0 0

Replies

replied on June 7, 2021

This solution is too buggy to use. I am back to looking for a way to use the setDefaults method on a specific calendar by class name. See video of how buggy this workaround of using an option instead is, depending on exactly where the user clicks, the calendar could get corrupted and show all days permanently. This doesn't happen when using setDefaults.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.