Flatpickr date not storing data with survey | XM Community
Solved

Flatpickr date not storing data with survey


Userlevel 4
Badge +9

As a follow up to my last post:

I got it to display beautifully, but the question doesn’t seem to be storing the date value after completing the survey. I’m not sure if somehow it has to do with using the Simple Layout option for a Theme or what… 

 I even commented out some stuff in case that was the reason, but it doesn’t seem to make a difference in whether it gets stored or not.

I even tried to store it as embedded data prior to submitting the survey and that didn’t make a difference either.

I don’t know how else to troubleshoot the data value not getting saved/stored with the survey response…. Please send help.

icon

Best answer by courtrc 6 March 2024, 21:14

View original

17 replies

Userlevel 5
Badge +22

Hey @courtrc , sorry to hear about the issues. Can you confirm if it’s at least inputting the information in-survey, just not saving? Or will nothing appear filled in when entering info on the survey?

@Tom_1842 may be able to assist more on this issue since he helped so much with your last inquiry!

Userlevel 7
Badge +27

Your jQuery selector is incorrect, so it isn’t finding the Qualtrics input field.  Replace .text-input with .InputText.

Userlevel 4
Badge +9

@tannerfaragher I’m not exactly sure what you’re asking but selecting the date from the Flatpickr in the survey does input a date into the field. I included a screenshot of that above. It’s just not saving. Every other piece of data in the survey saves, just not the date when submitting the survey.

I even tried to inspect the survey to see but I’m not technically savvy to look into if there’s something going wrong from when the date is showing as selected in the field and upon clicking submit.

Userlevel 5
Badge +22

@courtrc, thanks for the info, and sorry for the confusion on my reply. Yes, I was just checking to see if selecting the date from Flatpickr did at least put the date in the field. It sounds like Tom has a potential fix, hope it works! Y’all are awesome!

Userlevel 4
Badge +9

@TomG thank you so much! How would I know where to look for the correct jQuery selector name in the future?

I know how when inspecting the field I can see the class=”text-input” which was what I had in there from my previous posts’ suggested solution, but not sure how/where to look to know I’ve got the right jQuery selector.

 

Userlevel 4
Badge +9

@TomG unfortunately, that change actually made it so the Flatpickr doesn’t display at all now. I’m assuming something to do again with using the Simple Layout Theme.

 

Userlevel 7
Badge +27

Based on the screen shot, .text-input is correct. You have an atypical variety of Qualtrics - maybe related to simple layout. Therefore, I’m not sure why it isn’t saving.

I was going to mention that in your header, you are loading flatpickr twice. You only need to load the flatpickr css and flatpickr js once.

Userlevel 7
Badge +27

Hello! Sorry about that, I only tested getting Flatpickr to display, not saving the values.

This is indeed specific to Simple layout. I tried a couple methods to get the value to save to an input or embedded data field, but unfortunately none of them panned out.

Saving the value to an input field, either the input where Flatpickr is initialized or a separate/hidden input field that populates with the value of Flatpickr when a date is selected, did not work. The values get wiped away from the input fields as soon as the Next button is pressed. The value was retained in the input field IF the input field was clicked into and a keypress was recorded, but just setting the value with JS did not actually save it to the input. My attempts to simulate this keypress programmatically also did not pan out.

Saving the value to an embedded data field also did not work. When trying to use Qualtrics.SurveyEngine.setEmbeddedData, I encountered an error message in the console that I have not seen before. "setEmbeddedData deprecated, please use `setJSEmbeddedData`". I updated to Qualtrics.SurveyEngine.setJSEmbeddedData, but this does not appear to do anything at this time.

So I think currently with Simple Layout, inputs have to actually be typed into for the values to be saved. Without a way to save the Date value to an input field or to an Embedded Data field, I am not sure how to record the Flatpickr selection.
 

Userlevel 4
Badge +9

@Tom_1842 Appreciate all your attempts to solve this conundrum. I’ll just hold off for now and just resort to a text field with a date format validation which is a bummer cause Flatpickr is so slick.

I wish I knew how to mark this as Not Resolved anymore. But I don’t know who to even contact for that. I don’t think submitting a support ticket makes sense for that but idk who else to try to contact or if there’s a way to tag an admin user to this thread. 😒

Userlevel 7
Badge +27

Happy to help, and sounds good. @AmaraW is a Community Admin and might be able to help here.

Badge +48

@courtrc & @Tom_1842 - all fixed 😃

Userlevel 7
Badge +27

Hi @courtrc, so I found a hint on this page about setting Embedded Data and the below is working for me to store Flatpickr selections with Simple Layout. First, the hint:

setJSEmbeddedData(key, value)
Sets embedded data on the page. Only embedded data fields in the namespace _js can be set by this method.

The full process:

Add the below to the Survey's Header:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/themes/dark.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.js"></script>

Next, create a Text Entry question and add the below to the question's JavaScript in the OnReady section:

jQuery("#question-"+this.questionId+" .text-input").flatpickr({
dateFormat: "Y-m-d",
onChange: function(selectedDates, dateStr, instance) {
Qualtrics.SurveyEngine.setJSEmbeddedData("date1",dateStr);
}
});

Finally, in the Survey Flow, add the following Embedded Data field at the top of the Survey Flow:

__js_date1

The Flatpickr selection will be saved to the "__js_date1" Embedded Data field, even though it being set to “date1” in the JavaScript.

Userlevel 4
Badge +9

@Tom_1842 I’m not sure why I didn’t get notified of your replies. But appreciate it nonetheless!

 

I applied the jQuery and it works and stores the data in the embedded data when the question is optional. However, this question is required in my form and when I set the question as required, it throws an error and doesn’t let the form get submitted.

 

Any thoughts on this?
EDIT: When I look at Data & Analysis, it doesn’t show anything storing for that question, which makes sense…. 🤦🏼‍♀️ and how we got to this solution with the embedded data to begin with…So I’m not sure if there IS a solution other than making the field optional?

When field is optional, data storing:


When field is required, throwing an error.

 

Userlevel 4
Badge +9

Well I think I came up with a hack to get it to work.

Since the question is required, some value needs to be stored. So I set the question to have a default value of today’s date, so it stores that into the question to pass the requirement, but then stores the FlatPickr value into the embedded data because that’s the value I care about. 

 

When testing the field, the default value doesn’t actually display in the field, so the user doesn’t necessarily see that the question is storing today’s date. Which is inconsequential.

 

Userlevel 4
Badge +9

Combo answer:

This from @Tom_1842  

Hi @courtrc, so I found a hint on this page about setting Embedded Data and the below is working for me to store Flatpickr selections with Simple Layout. First, the hint:

setJSEmbeddedData(key, value)
Sets embedded data on the page. Only embedded data fields in the namespace _js can be set by this method.

The full process:

Add the below to the Survey's Header:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/themes/dark.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.js"></script>

Next, create a Text Entry question and add the below to the question's JavaScript in the OnReady section:

jQuery("#question-"+this.questionId+" .text-input").flatpickr({
dateFormat: "Y-m-d",
onChange: function(selectedDates, dateStr, instance) {
Qualtrics.SurveyEngine.setJSEmbeddedData("date1",dateStr);
}
});

Finally, in the Survey Flow, add the following Embedded Data field at the top of the Survey Flow:

__js_date1

The Flatpickr selection will be saved to the "__js_date1" Embedded Data field, even though it being set to “date1” in the JavaScript.

 

Plus this hack if your date field is required: 

Well I think I came up with a hack to get it to work.

Since the question is required, some value needs to be stored. So I set the question to have a default value of today’s date, so it stores that into the question to pass the requirement, but then stores the FlatPickr value into the embedded data because that’s the value I care about. 

 

When testing the field, the default value doesn’t actually display in the field, so the user doesn’t necessarily see that the question is storing today’s date. Which is inconsequential.

 

 

Userlevel 7
Badge +27

@courtrc Something I've noticed about Simple layout is that it forces clarity around which text data was created with JavaScript. Only being able to set Embedded Data to __js_ Embedded Data fields and being unable to set the value of text inputs in a way where the value is retained on page submit ensures that __js_ Embedded Data fields are the only fields that will contain text data created through custom JavaScript.

Since the Flatpickr selection is not being saved to the text input in a way that makes it to Qualtrics, it considers this field blank and throws a Force Response error. In your workaround, I don't think Force Response will ever trigger since the text input will always have a value in it through Default Choices.

Another approach to add Force Response to a Flatpickr text input is to have a hidden Multiple Choice question with Force Response enabled on the same page as the Flatpickr question that listens for any change to the text input and adds an answer to the MC question if a change is detected. The respondent can then only advance in the survey if a Flatpickr selection is made. To give it a try, create a Multiple Choice question after the Flatpickr text input, enable Force Response on it, and add the below to the MC question's JavaScript in the OnReady section:

//hide mulitple choice question
jQuery("#question-"+this.questionId).css({"display":"none"});

//define text input
jQuery("#question-"+this.questionId+" .text-input").eq(0).attr('id','text1');
var text1 = document.getElementById("text1");

//add event listener to text input to answer MC if text input is updated
var that=this;
text1.addEventListener('change', function () {
that.setChoiceValue(1, true);
});

 

Userlevel 7
Badge +27

Just want to update the JS in my earlier post for selecting the text input:

//hide mulitple choice question
jQuery("#question-"+this.questionId).css({"display":"none"});

//define text input
jQuery(".text-input").eq(0).attr('id','text1');
var text1 = document.getElementById("text1");

//add event listener to text input to answer MC if text input is updated
var that=this;
text1.addEventListener('change', function () {
that.setChoiceValue(1, true);
});

 

Leave a Reply